Prepare new, unofficial maemo release
[maemo-rb.git] / rbutil / sansapatcher / main.c
blob985e1b726277e3aed21b6fd728e65c990dad0682
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <inttypes.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include "sansapatcher.h"
32 #include "sansaio.h"
33 #include "parttypes.h"
34 #ifdef WITH_BOOTOBJS
35 #include "bootimg_c200.h"
36 #include "bootimg_e200.h"
37 #endif
39 #ifndef VERSION
40 #define VERSION "0.8 with v6.0 bootloaders"
41 #endif
43 enum {
44 NONE,
45 INSTALL,
46 INTERACTIVE,
47 SHOW_INFO,
48 LIST_IMAGES,
49 DELETE_BOOTLOADER,
50 ADD_BOOTLOADER,
51 READ_FIRMWARE,
52 WRITE_FIRMWARE,
53 READ_PARTITION,
54 WRITE_PARTITION,
55 UPDATE_OF,
56 UPDATE_PPBL
59 static void print_usage(void)
61 fprintf(stderr,"Usage: sansapatcher --scan\n");
62 #ifdef __WIN32__
63 fprintf(stderr," or sansapatcher [DISKNO] [action]\n");
64 #else
65 fprintf(stderr," or sansapatcher [device] [action]\n");
66 #endif
67 fprintf(stderr,"\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");
76 fprintf(stderr,"\n");
78 #ifdef __WIN32__
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");
83 fprintf(stderr,"\n");
84 #else
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");
91 #endif
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");
94 #endif
97 static const char* get_parttype(int pt)
99 int i;
100 static const char unknown[]="Unknown";
102 if (pt == -1) {
103 return "HFS/HFS+";
106 i=0;
107 while (parttypes[i].name != NULL) {
108 if (parttypes[i].type == pt) {
109 return (parttypes[i].name);
111 i++;
114 return unknown;
117 static void display_partinfo(struct sansa_t* sansa)
119 int i;
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[])
139 char yesno[4];
140 int i;
141 int n;
142 char* filename;
143 int action = SHOW_INFO;
144 struct sansa_t sansa;
145 int res = 0;
146 unsigned char* buf = NULL;
147 unsigned int len;
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))) {
154 print_usage();
155 return 1;
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");
165 return 0;
168 /* If the first parameter doesn't start with -, then we interpret it as a device */
169 if ((argc > 1) && (argv[1][0] != '-')) {
170 sansa.diskname[0]=0;
171 #ifdef __WIN32__
172 snprintf(sansa.diskname,sizeof(sansa.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
173 #else
174 strncpy(sansa.diskname,argv[1],sizeof(sansa.diskname));
175 #endif
176 i = 2;
177 } else {
178 /* Autoscan for C200/E200s */
179 n = sansa_scan(&sansa);
180 if (n==0) {
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__)
186 if (geteuid()!=0) {
187 fprintf(stderr,"[ERR] You may also need to run sansapatcher as root.\n");
189 #endif
190 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
191 } else if (n > 1) {
192 fprintf(stderr,"[ERR] %d Sansas found, aborting\n",n);
193 fprintf(stderr,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
196 if (n != 1) {
197 if (argc==1) {
198 printf("\nPress ENTER to exit sansapatcher :");
199 fgets(yesno,4,stdin);
201 return 0;
204 i = 1;
207 #ifdef WITH_BOOTOBJS
208 action = INTERACTIVE;
209 #endif
211 while (i < argc) {
212 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
213 action = LIST_IMAGES;
214 i++;
215 } else if (strcmp(argv[i],"--install")==0) {
216 action = INSTALL;
217 i++;
218 } else if ((strcmp(argv[i],"-d")==0) ||
219 (strcmp(argv[i],"--delete-bootloader")==0)) {
220 action = DELETE_BOOTLOADER;
221 i++;
222 } else if ((strcmp(argv[i],"-a")==0) ||
223 (strcmp(argv[i],"--add-bootloader")==0)) {
224 action = ADD_BOOTLOADER;
225 i++;
226 if (i == argc) { print_usage(); return 1; }
227 filename=argv[i];
228 i++;
229 } else if ((strcmp(argv[i],"-of")==0) ||
230 (strcmp(argv[i],"--update-original-firmware")==0)) {
231 action = UPDATE_OF;
232 i++;
233 if (i == argc) { print_usage(); return 1; }
234 filename=argv[i];
235 i++;
236 } else if ((strcmp(argv[i],"-bl")==0) ||
237 (strcmp(argv[i],"--update-ppbl")==0)) {
238 action = UPDATE_PPBL;
239 i++;
240 if (i == argc) { print_usage(); return 1; }
241 filename=argv[i];
242 i++;
243 } else if ((strcmp(argv[i],"-rf")==0) ||
244 (strcmp(argv[i],"--read-firmware")==0)) {
245 action = READ_FIRMWARE;
246 i++;
247 if (i == argc) { print_usage(); return 1; }
248 filename=argv[i];
249 i++;
253 if (sansa.diskname[0]==0) {
254 print_usage();
255 return 1;
258 if (sansa_open(&sansa, 0) < 0) {
259 return 1;
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) {
266 return 2;
269 display_partinfo(&sansa);
271 i = is_sansa(&sansa);
272 if (i < 0) {
273 fprintf(stderr,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i);
274 return 3;
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");
284 res = 4;
285 } else {
286 if (action==LIST_IMAGES) {
287 sansa_list_images(&sansa);
288 #ifdef WITH_BOOTOBJS
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)) {
294 if (yesno[0]=='i') {
295 if (sansa_reopen_rw(&sansa) < 0) {
296 res = 5;
298 if (strcmp(sansa.targetname,"c200") == 0) {
299 len = LEN_bootimg_c200;
300 buf = bootimg_c200;
301 } else {
302 len = LEN_bootimg_e200;
303 buf = bootimg_e200;
305 if (sansa_add_bootloader(&sansa, buf, len)==0) {
306 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
307 } else {
308 fprintf(stderr,"[ERR] --install failed.\n");
309 res = 6;
311 } else if (yesno[0]=='u') {
312 if (sansa_reopen_rw(&sansa) < 0) {
313 res = 5;
316 if (sansa_delete_bootloader(&sansa)==0) {
317 fprintf(stderr,"[INFO] Bootloader removed.\n");
318 } else {
319 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
320 res = 7;
324 #endif
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);
328 } else {
329 fprintf(stderr,"[ERR] --read-firmware failed.\n");
331 #ifdef WITH_BOOTOBJS
332 } else if (action==INSTALL) {
333 if (sansa_reopen_rw(&sansa) < 0) {
334 return 5;
337 if (strcmp(sansa.targetname,"c200") == 0) {
338 len = LEN_bootimg_c200;
339 buf = bootimg_c200;
340 } else {
341 len = LEN_bootimg_e200;
342 buf = bootimg_e200;
345 if (sansa_add_bootloader(&sansa, buf, len)==0) {
346 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
347 } else {
348 fprintf(stderr,"[ERR] --install failed.\n");
350 #endif
351 } else if (action==ADD_BOOTLOADER) {
352 if (sansa_reopen_rw(&sansa) < 0) {
353 return 5;
356 len = sansa_read_bootloader(&sansa, filename, &buf);
357 if (len > 0) {
358 if (sansa_add_bootloader(&sansa, buf, len)==0) {
359 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
360 } else {
361 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
364 } else if (action==DELETE_BOOTLOADER) {
365 if (sansa_reopen_rw(&sansa) < 0) {
366 return 5;
369 if (sansa_delete_bootloader(&sansa)==0) {
370 fprintf(stderr,"[INFO] Bootloader removed successfully.\n");
371 } else {
372 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
374 } else if (action==UPDATE_OF) {
375 if (sansa_reopen_rw(&sansa) < 0) {
376 return 5;
379 if (sansa_update_of(&sansa, filename)==0) {
380 fprintf(stderr,"[INFO] OF updated successfully.\n");
381 } else {
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)) {
391 if (yesno[0]=='y') {
392 if (sansa_reopen_rw(&sansa) < 0) {
393 return 5;
396 if (sansa_update_ppbl(&sansa, filename)==0) {
397 fprintf(stderr,"[INFO] PPBL updated successfully.\n");
398 } else {
399 fprintf(stderr,"[ERR] --update-ppbl failed.\n");
406 sansa_close(&sansa);
408 if (action==INTERACTIVE) {
409 printf("Press ENTER to exit sansapatcher :");
410 fgets(yesno,4,stdin);
413 return res;