summaryrefslogtreecommitdiff
path: root/utils/sansapatcher/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/sansapatcher/main.c')
-rw-r--r--utils/sansapatcher/main.c420
1 files changed, 420 insertions, 0 deletions
diff --git a/utils/sansapatcher/main.c b/utils/sansapatcher/main.c
new file mode 100644
index 0000000000..d12e33c22b
--- /dev/null
+++ b/utils/sansapatcher/main.c
@@ -0,0 +1,420 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006-2007 Dave Chapman
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
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>
30
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
38
39#ifndef VERSION
40#define VERSION "0.8 with v6.0 bootloaders"
41#endif
42
43enum {
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
57};
58
59static void print_usage(void)
60{
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");
77
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
95}
96
97static const char* get_parttype(int pt)
98{
99 int i;
100 static const char unknown[]="Unknown";
101
102 if (pt == -1) {
103 return "HFS/HFS+";
104 }
105
106 i=0;
107 while (parttypes[i].name != NULL) {
108 if (parttypes[i].type == pt) {
109 return (parttypes[i].name);
110 }
111 i++;
112 }
113
114 return unknown;
115}
116
117static void display_partinfo(struct sansa_t* sansa)
118{
119 int i;
120 double sectors_per_MB = (1024.0*1024.0)/sansa->sector_size;
121
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",
126 i,
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);
132 }
133 }
134}
135
136
137int main(int argc, char* argv[])
138{
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;
148
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");
152
153 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
154 print_usage();
155 return SANSA_OK;
156 }
157
158 if (sansa_alloc_buffer(&sansa, BUFFER_SIZE) < 0) {
159 fprintf(stderr,"Failed to allocate memory buffer\n");
160 return SANSA_INTERNAL_ERROR;
161 }
162
163 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
164 if (sansa_scan(&sansa) == 0)
165 fprintf(stderr,"[ERR] No E200s or C200s found.\n");
166 return SANSA_NOT_FOUND;
167 }
168
169 /* If the first parameter doesn't start with -, then we interpret it as a device */
170 if ((argc > 1) && (argv[1][0] != '-')) {
171 sansa.diskname[0]=0;
172#ifdef __WIN32__
173 snprintf(sansa.diskname,sizeof(sansa.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
174#else
175 strncpy(sansa.diskname,argv[1],sizeof(sansa.diskname));
176#endif
177 i = 2;
178 } else {
179 /* Autoscan for C200/E200s */
180 n = sansa_scan(&sansa);
181 if (n==0) {
182 fprintf(stderr,"[ERR] No E200s or C200s found, aborting\n");
183 fprintf(stderr,"[ERR] Please connect your sansa and ensure it is in UMS mode\n");
184#if defined(__APPLE__) && defined(__MACH__)
185 fprintf(stderr,"[ERR] Also ensure that your Sansa's main partition is not mounted.\n");
186#elif !defined(__WIN32__)
187 if (geteuid()!=0) {
188 fprintf(stderr,"[ERR] You may also need to run sansapatcher as root.\n");
189 }
190#endif
191 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
192 } else if (n > 1) {
193 fprintf(stderr,"[ERR] %d Sansas found, aborting\n",n);
194 fprintf(stderr,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
195 }
196
197 if (n != 1) {
198#ifdef WITH_BOOTOBJS
199 if (argc==1) {
200 printf("\nPress ENTER to exit sansapatcher :");
201 fgets(yesno,4,stdin);
202 }
203#endif
204 return n > 1 ? SANSA_MULTIPLE_DEVICES : SANSA_NOT_FOUND;
205 }
206
207 i = 1;
208 }
209
210#ifdef WITH_BOOTOBJS
211 action = INTERACTIVE;
212#endif
213
214 while (i < argc) {
215 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
216 action = LIST_IMAGES;
217 i++;
218 } else if (strcmp(argv[i],"--install")==0) {
219 action = INSTALL;
220 i++;
221 } else if ((strcmp(argv[i],"-d")==0) ||
222 (strcmp(argv[i],"--delete-bootloader")==0)) {
223 action = DELETE_BOOTLOADER;
224 i++;
225 } else if ((strcmp(argv[i],"-a")==0) ||
226 (strcmp(argv[i],"--add-bootloader")==0)) {
227 action = ADD_BOOTLOADER;
228 i++;
229 if (i == argc) { print_usage(); return SANSA_WRONG_ARGUMENTS; }
230 filename=argv[i];
231 i++;
232 } else if ((strcmp(argv[i],"-of")==0) ||
233 (strcmp(argv[i],"--update-original-firmware")==0)) {
234 action = UPDATE_OF;
235 i++;
236 if (i == argc) { print_usage(); return SANSA_WRONG_ARGUMENTS; }
237 filename=argv[i];
238 i++;
239 } else if ((strcmp(argv[i],"-bl")==0) ||
240 (strcmp(argv[i],"--update-ppbl")==0)) {
241 action = UPDATE_PPBL;
242 i++;
243 if (i == argc) { print_usage(); return SANSA_WRONG_ARGUMENTS; }
244 filename=argv[i];
245 i++;
246 } else if ((strcmp(argv[i],"-rf")==0) ||
247 (strcmp(argv[i],"--read-firmware")==0)) {
248 action = READ_FIRMWARE;
249 i++;
250 if (i == argc) { print_usage(); return SANSA_WRONG_ARGUMENTS; }
251 filename=argv[i];
252 i++;
253 }
254 }
255
256 if (sansa.diskname[0]==0) {
257 print_usage();
258 return SANSA_WRONG_ARGUMENTS;
259 }
260
261 if (sansa_open(&sansa, 0) < 0) {
262 return SANSA_ACCESS_DENIED;
263 }
264
265 fprintf(stderr,"[INFO] Reading partition table from %s\n",sansa.diskname);
266 fprintf(stderr,"[INFO] Sector size is %d bytes\n",sansa.sector_size);
267
268 if (sansa_read_partinfo(&sansa,0) < 0) {
269 return SANSA_PARTITION_ERROR;
270 }
271
272 display_partinfo(&sansa);
273
274 i = is_sansa(&sansa);
275 if (i < 0) {
276 fprintf(stderr,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i);
277 return SANSA_WRONG_TYPE;
278 }
279
280 if (sansa.hasoldbootloader) {
281 printf("[ERR] ************************************************************************\n");
282 printf("[ERR] *** OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n");
283 printf("[ERR] *** You must reinstall the original Sansa firmware before running\n");
284 printf("[ERR] *** sansapatcher for the first time.\n");
285 printf("[ERR] *** See http://www.rockbox.org/wiki/SansaE200Install\n");
286 printf("[ERR] ************************************************************************\n");
287 res = SANSA_OLD_INSTALL;
288 } else {
289 if (action==LIST_IMAGES) {
290 sansa_list_images(&sansa);
291#ifdef WITH_BOOTOBJS
292 } else if (action==INTERACTIVE) {
293
294 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
295
296 if (fgets(yesno,4,stdin)) {
297 if (yesno[0]=='i') {
298 if (sansa_reopen_rw(&sansa) < 0) {
299 res = SANSA_CANNOT_REOPEN;
300 }
301 if (strcmp(sansa.targetname,"c200") == 0) {
302 len = LEN_bootimg_c200;
303 buf = bootimg_c200;
304 } else {
305 len = LEN_bootimg_e200;
306 buf = bootimg_e200;
307 }
308 if (sansa_add_bootloader(&sansa, buf, len)==0) {
309 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
310 } else {
311 fprintf(stderr,"[ERR] --install failed.\n");
312 res = SANSA_INSTALL_FAILED;
313 }
314 } else if (yesno[0]=='u') {
315 if (sansa_reopen_rw(&sansa) < 0) {
316 res = SANSA_CANNOT_REOPEN;
317 }
318
319 if (sansa_delete_bootloader(&sansa)==0) {
320 fprintf(stderr,"[INFO] Bootloader removed.\n");
321 } else {
322 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
323 res = SANSA_UNINSTALL_FAILED;
324 }
325 }
326 }
327#endif
328 } else if (action==READ_FIRMWARE) {
329 if (sansa_read_firmware(&sansa, filename)==0) {
330 fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename);
331 } else {
332 fprintf(stderr,"[ERR] --read-firmware failed.\n");
333 }
334#ifdef WITH_BOOTOBJS
335 } else if (action==INSTALL) {
336 if (sansa_reopen_rw(&sansa) < 0) {
337 return SANSA_CANNOT_REOPEN;
338 }
339
340 if (strcmp(sansa.targetname,"c200") == 0) {
341 len = LEN_bootimg_c200;
342 buf = bootimg_c200;
343 } else {
344 len = LEN_bootimg_e200;
345 buf = bootimg_e200;
346 }
347
348 if (sansa_add_bootloader(&sansa, buf, len)==0) {
349 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
350 } else {
351 fprintf(stderr,"[ERR] --install failed.\n");
352 }
353#endif
354 } else if (action==ADD_BOOTLOADER) {
355 if (sansa_reopen_rw(&sansa) < 0) {
356 return SANSA_CANNOT_REOPEN;
357 }
358
359 len = sansa_read_bootloader(&sansa, filename, &buf);
360 if (len > 0) {
361 if (sansa_add_bootloader(&sansa, buf, len)==0) {
362 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
363 } else {
364 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
365 }
366 }
367 } else if (action==DELETE_BOOTLOADER) {
368 if (sansa_reopen_rw(&sansa) < 0) {
369 return SANSA_CANNOT_REOPEN;
370 }
371
372 if (sansa_delete_bootloader(&sansa)==0) {
373 fprintf(stderr,"[INFO] Bootloader removed successfully.\n");
374 } else {
375 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
376 }
377 } else if (action==UPDATE_OF) {
378 if (sansa_reopen_rw(&sansa) < 0) {
379 return SANSA_CANNOT_REOPEN;
380 }
381
382 if (sansa_update_of(&sansa, filename)==0) {
383 fprintf(stderr,"[INFO] OF updated successfully.\n");
384 } else {
385 fprintf(stderr,"[ERR] --update-original-firmware failed.\n");
386 }
387 } else if (action==UPDATE_PPBL) {
388 printf("[WARN] PPBL installation will overwrite your bootloader. This will lead to a\n");
389 printf(" Sansa that won't boot if the bootloader file is invalid. Only continue if\n");
390 printf(" you're sure you know what you're doing.\n");
391 printf(" Continue (y/n)? ");
392
393 if (fgets(yesno,4,stdin)) {
394 if (yesno[0]=='y') {
395 if (sansa_reopen_rw(&sansa) < 0) {
396 return SANSA_CANNOT_REOPEN;
397 }
398
399 if (sansa_update_ppbl(&sansa, filename)==0) {
400 fprintf(stderr,"[INFO] PPBL updated successfully.\n");
401 } else {
402 fprintf(stderr,"[ERR] --update-ppbl failed.\n");
403 }
404 }
405 }
406 }
407 }
408
409 sansa_close(&sansa);
410 sansa_dealloc_buffer(&sansa);
411
412#ifdef WITH_BOOTOBJS
413 if (action==INTERACTIVE) {
414 printf("Press ENTER to exit sansapatcher :");
415 fgets(yesno,4,stdin);
416 }
417#endif
418
419 return res;
420}