blob: b813fadbe6791d7a09f1bca47250659cfdab8036 [file] [log] [blame]
Dave Chapmane17043e2007-03-15 22:55:36 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Dave Chapman4a812912007-03-15 23:02:37 +00008 * $Id$
Dave Chapmane17043e2007-03-15 22:55:36 +00009 *
10 * Copyright (C) 2006-2007 Dave Chapman
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
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>
28
29#include "sansapatcher.h"
30#include "sansaio.h"
Dave Chapmanbe297162007-03-16 00:23:23 +000031#include "parttypes.h"
Dave Chapmane17043e2007-03-15 22:55:36 +000032
Barry Wardell4991cd32007-10-20 19:12:16 +000033#define VERSION "0.6 with v4.0 bootloaders"
Dave Chapmane17043e2007-03-15 22:55:36 +000034
Dave Chapman0658dce2007-05-03 21:05:24 +000035unsigned char* sectorbuf;
36
Dave Chapmane17043e2007-03-15 22:55:36 +000037int verbose = 0;
38
39enum {
40 NONE,
Dave Chapmane17043e2007-03-15 22:55:36 +000041 INSTALL,
Dave Chapmane17043e2007-03-15 22:55:36 +000042 INTERACTIVE,
43 SHOW_INFO,
44 LIST_IMAGES,
45 DELETE_BOOTLOADER,
46 ADD_BOOTLOADER,
47 READ_FIRMWARE,
48 WRITE_FIRMWARE,
49 READ_PARTITION,
Barry Wardell6a0ec8b2007-08-02 11:39:43 +000050 WRITE_PARTITION,
Barry Wardell080889a2007-10-14 18:09:40 +000051 UPDATE_OF,
52 UPDATE_PPBL
Dave Chapmane17043e2007-03-15 22:55:36 +000053};
54
55void print_usage(void)
56{
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");
Dave Chapmane17043e2007-03-15 22:55:36 +000065 fprintf(stderr," --install\n");
Dave Chapmane17043e2007-03-15 22:55:36 +000066 fprintf(stderr," -l, --list\n");
Barry Wardell6a0ec8b2007-08-02 11:39:43 +000067 fprintf(stderr," -rf, --read-firmware filename.mi4\n");
68 fprintf(stderr," -a, --add-bootloader filename.mi4\n");
Dave Chapmane17043e2007-03-15 22:55:36 +000069 fprintf(stderr," -d, --delete-bootloader\n");
Barry Wardell6a0ec8b2007-08-02 11:39:43 +000070 fprintf(stderr," -of --update-original-firmware filename.mi4\n");
Barry Wardell080889a2007-10-14 18:09:40 +000071 fprintf(stderr," -bl --update-ppbl filename.bin\n");
Dave Chapmane17043e2007-03-15 22:55:36 +000072 fprintf(stderr,"\n");
73
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");
Dave Chapman8d145a52007-09-19 18:46:54 +000078 fprintf(stderr,"can identify it as being an E200 or C200.\n");
Dave Chapmane17043e2007-03-15 22:55:36 +000079 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");
Dave Chapman8d145a52007-09-19 18:46:54 +000089 fprintf(stderr,"an E200 or C200.\n");
Dave Chapmane17043e2007-03-15 22:55:36 +000090#endif
91}
92
Dave Chapmanbe297162007-03-16 00:23:23 +000093char* get_parttype(int pt)
94{
95 int i;
96 static char unknown[]="Unknown";
97
98 if (pt == -1) {
99 return "HFS/HFS+";
100 }
101
102 i=0;
103 while (parttypes[i].name != NULL) {
104 if (parttypes[i].type == pt) {
105 return (parttypes[i].name);
106 }
107 i++;
108 }
109
110 return unknown;
111}
112
Dave Chapmane17043e2007-03-15 22:55:36 +0000113void display_partinfo(struct sansa_t* sansa)
114{
115 int i;
116 double sectors_per_MB = (1024.0*1024.0)/sansa->sector_size;
117
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",
122 i,
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);
128 }
129 }
130}
131
132
133int main(int argc, char* argv[])
134{
Dave Chapmane17043e2007-03-15 22:55:36 +0000135 char yesno[4];
Dave Chapmane17043e2007-03-15 22:55:36 +0000136 int i;
137 int n;
138 char* filename;
139 int action = SHOW_INFO;
140 int type;
141 struct sansa_t sansa;
Dave Chapman5efce182007-03-21 11:05:38 +0000142 int res = 0;
Dave Chapmane17043e2007-03-15 22:55:36 +0000143
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");
147
148 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
149 print_usage();
150 return 1;
151 }
152
153 if (sansa_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) {
154 fprintf(stderr,"Failed to allocate memory buffer\n");
155 }
156
157 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
158 if (sansa_scan(&sansa) == 0)
Dave Chapman8d145a52007-09-19 18:46:54 +0000159 fprintf(stderr,"[ERR] No E200s or C200s found.\n");
Dave Chapmane17043e2007-03-15 22:55:36 +0000160 return 0;
161 }
162
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 {
Dave Chapman8d145a52007-09-19 18:46:54 +0000173 /* Autoscan for C200/E200s */
Dave Chapmane17043e2007-03-15 22:55:36 +0000174 n = sansa_scan(&sansa);
175 if (n==0) {
Dave Chapman8d145a52007-09-19 18:46:54 +0000176 fprintf(stderr,"[ERR] No E200s or C200s found, aborting\n");
Dave Chapmane17043e2007-03-15 22:55:36 +0000177 fprintf(stderr,"[ERR] Please connect your sansa and ensure it is in UMS mode\n");
178#if defined(__APPLE__) && defined(__MACH__)
Dave Chapman8d145a52007-09-19 18:46:54 +0000179 fprintf(stderr,"[ERR] Also ensure that your Sansa's main partition is not mounted.\n");
Dave Chapmane17043e2007-03-15 22:55:36 +0000180#elif !defined(__WIN32__)
181 if (geteuid()!=0) {
182 fprintf(stderr,"[ERR] You may also need to run sansapatcher as root.\n");
183 }
184#endif
185 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
186 } else if (n > 1) {
Dave Chapman8d145a52007-09-19 18:46:54 +0000187 fprintf(stderr,"[ERR] %d Sansas found, aborting\n",n);
188 fprintf(stderr,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
Dave Chapmane17043e2007-03-15 22:55:36 +0000189 }
190
191 if (n != 1) {
Dave Chapmane17043e2007-03-15 22:55:36 +0000192 if (argc==1) {
193 printf("\nPress ENTER to exit sansapatcher :");
194 fgets(yesno,4,stdin);
195 }
Dave Chapmane17043e2007-03-15 22:55:36 +0000196 return 0;
197 }
198
199 i = 1;
200 }
201
Dave Chapmane17043e2007-03-15 22:55:36 +0000202 action = INTERACTIVE;
Dave Chapmane17043e2007-03-15 22:55:36 +0000203
204 while (i < argc) {
205 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
206 action = LIST_IMAGES;
207 i++;
Dave Chapmane17043e2007-03-15 22:55:36 +0000208 } else if (strcmp(argv[i],"--install")==0) {
209 action = INSTALL;
210 i++;
Dave Chapmane17043e2007-03-15 22:55:36 +0000211 } 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++;
Barry Wardell6a0ec8b2007-08-02 11:39:43 +0000223 } 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++;
Barry Wardell080889a2007-10-14 18:09:40 +0000230 } 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++;
Dave Chapmane17043e2007-03-15 22:55:36 +0000237 } 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++;
244 }
245 }
246
247 if (sansa.diskname[0]==0) {
248 print_usage();
249 return 1;
250 }
251
252 if (sansa_open(&sansa, 0) < 0) {
253 return 1;
254 }
255
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);
258
Dave Chapmanbe297162007-03-16 00:23:23 +0000259 if (sansa_read_partinfo(&sansa,0) < 0) {
Dave Chapmane17043e2007-03-15 22:55:36 +0000260 return 2;
261 }
262
263 display_partinfo(&sansa);
264
Dave Chapman8d145a52007-09-19 18:46:54 +0000265 i = is_sansa(&sansa);
Dave Chapmane17043e2007-03-15 22:55:36 +0000266 if (i < 0) {
Dave Chapman8d145a52007-09-19 18:46:54 +0000267 fprintf(stderr,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i);
Dave Chapmane17043e2007-03-15 22:55:36 +0000268 return 3;
269 }
270
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");
Dave Chapman5efce182007-03-21 11:05:38 +0000278 res = 4;
279 } else {
280 if (action==LIST_IMAGES) {
281 sansa_list_images(&sansa);
282 } else if (action==INTERACTIVE) {
Dave Chapmane17043e2007-03-15 22:55:36 +0000283
Dave Chapman5efce182007-03-21 11:05:38 +0000284 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
Dave Chapmane8156012007-03-15 23:26:47 +0000285
Dave Chapman5efce182007-03-21 11:05:38 +0000286 if (fgets(yesno,4,stdin)) {
287 if (yesno[0]=='i') {
288 if (sansa_reopen_rw(&sansa) < 0) {
289 res = 5;
290 }
Dave Chapmane8156012007-03-15 23:26:47 +0000291
Dave Chapman5efce182007-03-21 11:05:38 +0000292 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;
297 }
298 } else if (yesno[0]=='u') {
299 if (sansa_reopen_rw(&sansa) < 0) {
300 res = 5;
301 }
Dave Chapmane8156012007-03-15 23:26:47 +0000302
Dave Chapman5efce182007-03-21 11:05:38 +0000303 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;
308 }
Dave Chapmane8156012007-03-15 23:26:47 +0000309 }
310 }
Dave Chapman5efce182007-03-21 11:05:38 +0000311 } 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");
316 }
317 } else if (action==INSTALL) {
318 if (sansa_reopen_rw(&sansa) < 0) {
319 return 5;
320 }
Dave Chapmane8156012007-03-15 23:26:47 +0000321
Dave Chapman5efce182007-03-21 11:05:38 +0000322 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");
326 }
327 } else if (action==ADD_BOOTLOADER) {
328 if (sansa_reopen_rw(&sansa) < 0) {
329 return 5;
330 }
Dave Chapmane17043e2007-03-15 22:55:36 +0000331
Dave Chapman5efce182007-03-21 11:05:38 +0000332 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");
336 }
337 } else if (action==DELETE_BOOTLOADER) {
338 if (sansa_reopen_rw(&sansa) < 0) {
339 return 5;
340 }
Dave Chapmane17043e2007-03-15 22:55:36 +0000341
Dave Chapman5efce182007-03-21 11:05:38 +0000342 if (sansa_delete_bootloader(&sansa)==0) {
343 fprintf(stderr,"[INFO] Bootloader removed successfully.\n");
344 } else {
345 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
346 }
Barry Wardell6a0ec8b2007-08-02 11:39:43 +0000347 } else if (action==UPDATE_OF) {
348 if (sansa_reopen_rw(&sansa) < 0) {
349 return 5;
350 }
351
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");
356 }
Barry Wardell080889a2007-10-14 18:09:40 +0000357 } 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)? ");
362
363 if (fgets(yesno,4,stdin)) {
364 if (yesno[0]=='y') {
365 if (sansa_reopen_rw(&sansa) < 0) {
366 return 5;
367 }
368
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");
373 }
374 }
375 }
Dave Chapmane17043e2007-03-15 22:55:36 +0000376 }
377 }
378
379 sansa_close(&sansa);
380
Dave Chapmane17043e2007-03-15 22:55:36 +0000381 if (action==INTERACTIVE) {
382 printf("Press ENTER to exit sansapatcher :");
383 fgets(yesno,4,stdin);
384 }
Dave Chapmane17043e2007-03-15 22:55:36 +0000385
Dave Chapman5efce182007-03-21 11:05:38 +0000386 return res;
Dave Chapmane17043e2007-03-15 22:55:36 +0000387}