Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / rbutil / sansapatcher / main.c
blob4e95ae82b2dd5c6982f73915b9aa68802edae6e3
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"
35 #define VERSION "0.6 with v4.0 bootloaders"
37 enum {
38 NONE,
39 INSTALL,
40 INTERACTIVE,
41 SHOW_INFO,
42 LIST_IMAGES,
43 DELETE_BOOTLOADER,
44 ADD_BOOTLOADER,
45 READ_FIRMWARE,
46 WRITE_FIRMWARE,
47 READ_PARTITION,
48 WRITE_PARTITION,
49 UPDATE_OF,
50 UPDATE_PPBL
53 void print_usage(void)
55 fprintf(stderr,"Usage: sansapatcher --scan\n");
56 #ifdef __WIN32__
57 fprintf(stderr," or sansapatcher [DISKNO] [action]\n");
58 #else
59 fprintf(stderr," or sansapatcher [device] [action]\n");
60 #endif
61 fprintf(stderr,"\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");
70 fprintf(stderr,"\n");
72 #ifdef __WIN32__
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");
77 fprintf(stderr,"\n");
78 #else
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");
85 #endif
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");
88 #endif
91 char* get_parttype(int pt)
93 int i;
94 static char unknown[]="Unknown";
96 if (pt == -1) {
97 return "HFS/HFS+";
100 i=0;
101 while (parttypes[i].name != NULL) {
102 if (parttypes[i].type == pt) {
103 return (parttypes[i].name);
105 i++;
108 return unknown;
111 void display_partinfo(struct sansa_t* sansa)
113 int i;
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[])
133 char yesno[4];
134 int i;
135 int n;
136 char* filename;
137 int action = SHOW_INFO;
138 int type;
139 struct sansa_t sansa;
140 int res = 0;
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))) {
147 print_usage();
148 return 1;
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");
158 return 0;
161 /* If the first parameter doesn't start with -, then we interpret it as a device */
162 if ((argc > 1) && (argv[1][0] != '-')) {
163 sansa.diskname[0]=0;
164 #ifdef __WIN32__
165 snprintf(sansa.diskname,sizeof(sansa.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
166 #else
167 strncpy(sansa.diskname,argv[1],sizeof(sansa.diskname));
168 #endif
169 i = 2;
170 } else {
171 /* Autoscan for C200/E200s */
172 n = sansa_scan(&sansa);
173 if (n==0) {
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__)
179 if (geteuid()!=0) {
180 fprintf(stderr,"[ERR] You may also need to run sansapatcher as root.\n");
182 #endif
183 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
184 } else if (n > 1) {
185 fprintf(stderr,"[ERR] %d Sansas found, aborting\n",n);
186 fprintf(stderr,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
189 if (n != 1) {
190 if (argc==1) {
191 printf("\nPress ENTER to exit sansapatcher :");
192 fgets(yesno,4,stdin);
194 return 0;
197 i = 1;
200 action = INTERACTIVE;
202 while (i < argc) {
203 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
204 action = LIST_IMAGES;
205 i++;
206 } else if (strcmp(argv[i],"--install")==0) {
207 action = INSTALL;
208 i++;
209 } else if ((strcmp(argv[i],"-d")==0) ||
210 (strcmp(argv[i],"--delete-bootloader")==0)) {
211 action = DELETE_BOOTLOADER;
212 i++;
213 } else if ((strcmp(argv[i],"-a")==0) ||
214 (strcmp(argv[i],"--add-bootloader")==0)) {
215 action = ADD_BOOTLOADER;
216 type = FILETYPE_MI4;
217 i++;
218 if (i == argc) { print_usage(); return 1; }
219 filename=argv[i];
220 i++;
221 } else if ((strcmp(argv[i],"-of")==0) ||
222 (strcmp(argv[i],"--update-original-firmware")==0)) {
223 action = UPDATE_OF;
224 i++;
225 if (i == argc) { print_usage(); return 1; }
226 filename=argv[i];
227 i++;
228 } else if ((strcmp(argv[i],"-bl")==0) ||
229 (strcmp(argv[i],"--update-ppbl")==0)) {
230 action = UPDATE_PPBL;
231 i++;
232 if (i == argc) { print_usage(); return 1; }
233 filename=argv[i];
234 i++;
235 } else if ((strcmp(argv[i],"-rf")==0) ||
236 (strcmp(argv[i],"--read-firmware")==0)) {
237 action = READ_FIRMWARE;
238 i++;
239 if (i == argc) { print_usage(); return 1; }
240 filename=argv[i];
241 i++;
245 if (sansa.diskname[0]==0) {
246 print_usage();
247 return 1;
250 if (sansa_open(&sansa, 0) < 0) {
251 return 1;
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) {
258 return 2;
261 display_partinfo(&sansa);
263 i = is_sansa(&sansa);
264 if (i < 0) {
265 fprintf(stderr,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i);
266 return 3;
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");
276 res = 4;
277 } else {
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)) {
285 if (yesno[0]=='i') {
286 if (sansa_reopen_rw(&sansa) < 0) {
287 res = 5;
290 if (sansa_add_bootloader(&sansa, NULL, FILETYPE_INTERNAL)==0) {
291 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
292 } else {
293 fprintf(stderr,"[ERR] --install failed.\n");
294 res = 6;
296 } else if (yesno[0]=='u') {
297 if (sansa_reopen_rw(&sansa) < 0) {
298 res = 5;
301 if (sansa_delete_bootloader(&sansa)==0) {
302 fprintf(stderr,"[INFO] Bootloader removed.\n");
303 } else {
304 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
305 res = 7;
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);
312 } else {
313 fprintf(stderr,"[ERR] --read-firmware failed.\n");
315 } else if (action==INSTALL) {
316 if (sansa_reopen_rw(&sansa) < 0) {
317 return 5;
320 if (sansa_add_bootloader(&sansa, NULL, FILETYPE_INTERNAL)==0) {
321 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
322 } else {
323 fprintf(stderr,"[ERR] --install failed.\n");
325 } else if (action==ADD_BOOTLOADER) {
326 if (sansa_reopen_rw(&sansa) < 0) {
327 return 5;
330 if (sansa_add_bootloader(&sansa, filename, type)==0) {
331 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
332 } else {
333 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
335 } else if (action==DELETE_BOOTLOADER) {
336 if (sansa_reopen_rw(&sansa) < 0) {
337 return 5;
340 if (sansa_delete_bootloader(&sansa)==0) {
341 fprintf(stderr,"[INFO] Bootloader removed successfully.\n");
342 } else {
343 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
345 } else if (action==UPDATE_OF) {
346 if (sansa_reopen_rw(&sansa) < 0) {
347 return 5;
350 if (sansa_update_of(&sansa, filename)==0) {
351 fprintf(stderr,"[INFO] OF updated successfully.\n");
352 } else {
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)) {
362 if (yesno[0]=='y') {
363 if (sansa_reopen_rw(&sansa) < 0) {
364 return 5;
367 if (sansa_update_ppbl(&sansa, filename)==0) {
368 fprintf(stderr,"[INFO] PPBL updated successfully.\n");
369 } else {
370 fprintf(stderr,"[ERR] --update-ppbl failed.\n");
377 sansa_close(&sansa);
379 if (action==INTERACTIVE) {
380 printf("Press ENTER to exit sansapatcher :");
381 fgets(yesno,4,stdin);
384 return res;