Add clix screenshots to the manual.
[kugel-rb.git] / rbutil / ipodpatcher / main.c
blob1dcb91624078f104c1b16e384383636c0673ada9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: ipodpatcher.c 12237 2007-02-08 21:31:38Z dave $
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 "ipodpatcher.h"
32 #include "ipodio.h"
34 #define VERSION "3.0 with v3.0 bootloaders"
36 enum {
37 NONE,
38 #ifdef WITH_BOOTOBJS
39 INSTALL,
40 #endif
41 INTERACTIVE,
42 SHOW_INFO,
43 LIST_IMAGES,
44 DELETE_BOOTLOADER,
45 ADD_BOOTLOADER,
46 READ_FIRMWARE,
47 WRITE_FIRMWARE,
48 READ_AUPD,
49 WRITE_AUPD,
50 READ_PARTITION,
51 WRITE_PARTITION,
52 FORMAT_PARTITION,
53 DUMP_XML,
54 CONVERT_TO_FAT32
57 void print_macpod_warning(void)
59 printf("[INFO] ************************************************************************\n");
60 printf("[INFO] *** WARNING FOR ROCKBOX USERS\n");
61 printf("[INFO] *** You must convert this ipod to FAT32 format (aka a \"winpod\")\n");
62 printf("[INFO] *** if you want to run Rockbox. Rockbox WILL NOT work on this ipod.\n");
63 printf("[INFO] *** See http://www.rockbox.org/twiki/bin/view/Main/IpodConversionToFAT32\n");
64 printf("[INFO] ************************************************************************\n");
67 void print_usage(void)
69 fprintf(stderr,"Usage: ipodpatcher --scan\n");
70 #ifdef __WIN32__
71 fprintf(stderr," or ipodpatcher [DISKNO] [action]\n");
72 #else
73 fprintf(stderr," or ipodpatcher [device] [action]\n");
74 #endif
75 fprintf(stderr,"\n");
76 fprintf(stderr,"Where [action] is one of the following options:\n");
77 #ifdef WITH_BOOTOBJS
78 fprintf(stderr," --install\n");
79 #endif
80 fprintf(stderr," -l, --list\n");
81 fprintf(stderr," -r, --read-partition bootpartition.bin\n");
82 fprintf(stderr," -w, --write-partition bootpartition.bin\n");
83 fprintf(stderr," -rf, --read-firmware filename.ipod\n");
84 fprintf(stderr," -rfb, --read-firmware-bin filename.bin\n");
85 fprintf(stderr," -wf, --write-firmware filename.ipod\n");
86 fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n");
87 #ifdef WITH_BOOTOBJS
88 fprintf(stderr," -we, --write-embedded\n");
89 #endif
90 fprintf(stderr," -a, --add-bootloader filename.ipod\n");
91 fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n");
92 fprintf(stderr," -d, --delete-bootloader\n");
93 fprintf(stderr," -f, --format\n");
94 fprintf(stderr," -c, --convert\n");
95 fprintf(stderr," --read-aupd filename.bin\n");
96 fprintf(stderr," --write-aupd filename.bin\n");
97 fprintf(stderr," -x --dump-xml filename.xml\n");
98 fprintf(stderr,"\n");
100 #ifdef __WIN32__
101 fprintf(stderr,"DISKNO is the number (e.g. 2) Windows has assigned to your ipod's hard disk.\n");
102 fprintf(stderr,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
103 fprintf(stderr,"will be disk 1 etc. ipodpatcher will refuse to access a disk unless it\n");
104 fprintf(stderr,"can identify it as being an ipod.\n");
105 fprintf(stderr,"\n");
106 #else
107 #if defined(linux) || defined (__linux)
108 fprintf(stderr,"\"device\" is the device node (e.g. /dev/sda) assigned to your ipod.\n");
109 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
110 fprintf(stderr,"\"device\" is the device node (e.g. /dev/da1) assigned to your ipod.\n");
111 #elif defined(__APPLE__) && defined(__MACH__)
112 fprintf(stderr,"\"device\" is the device node (e.g. /dev/disk1) assigned to your ipod.\n");
113 #endif
114 fprintf(stderr,"ipodpatcher will refuse to access a disk unless it can identify it as being\n");
115 fprintf(stderr,"an ipod.\n");
116 #endif
119 void display_partinfo(struct ipod_t* ipod)
121 int i;
122 double sectors_per_MB = (1024.0*1024.0)/ipod->sector_size;
124 printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
125 for ( i = 0; i < 4; i++ ) {
126 if (ipod->pinfo[i].start != 0) {
127 printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
129 (long int)ipod->pinfo[i].start,
130 (long int)ipod->pinfo[i].start+ipod->pinfo[i].size-1,
131 ipod->pinfo[i].size/sectors_per_MB,
132 get_parttype(ipod->pinfo[i].type),
133 (int)ipod->pinfo[i].type);
139 int main(int argc, char* argv[])
141 char yesno[4];
142 int i;
143 int n;
144 int infile, outfile;
145 unsigned int inputsize;
146 char* filename;
147 int action = SHOW_INFO;
148 int type;
149 struct ipod_t ipod;
151 fprintf(stderr,"ipodpatcher v" VERSION " - (C) Dave Chapman 2006-2007\n");
152 fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n");
153 fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
155 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
156 print_usage();
157 return 1;
160 if (ipod_alloc_buffer(&ipod_sectorbuf,BUFFER_SIZE) < 0) {
161 fprintf(stderr,"Failed to allocate memory buffer\n");
164 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
165 if (ipod_scan(&ipod) == 0)
166 fprintf(stderr,"[ERR] No ipods found.\n");
167 return 0;
170 /* If the first parameter doesn't start with -, then we interpret it as a device */
171 if ((argc > 1) && (argv[1][0] != '-')) {
172 ipod.diskname[0]=0;
173 #ifdef __WIN32__
174 snprintf(ipod.diskname,sizeof(ipod.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
175 #else
176 strncpy(ipod.diskname,argv[1],sizeof(ipod.diskname));
177 #endif
178 i = 2;
179 } else {
180 /* Autoscan for ipods */
181 n = ipod_scan(&ipod);
182 if (n==0) {
183 fprintf(stderr,"[ERR] No ipods found, aborting\n");
184 fprintf(stderr,"[ERR] Please connect your ipod and ensure it is in disk mode\n");
185 #if defined(__APPLE__) && defined(__MACH__)
186 fprintf(stderr,"[ERR] Also ensure that itunes is closed, and that your ipod is not mounted.\n");
187 #elif !defined(__WIN32__)
188 if (geteuid()!=0) {
189 fprintf(stderr,"[ERR] You may also need to run ipodpatcher as root.\n");
191 #endif
192 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
193 } else if (n > 1) {
194 fprintf(stderr,"[ERR] %d ipods found, aborting\n",n);
195 fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\n");
198 if (n != 1) {
199 #ifdef WITH_BOOTOBJS
200 if (argc==1) {
201 printf("\nPress ENTER to exit ipodpatcher :");
202 fgets(yesno,4,stdin);
204 #endif
205 return 0;
208 i = 1;
211 #ifdef WITH_BOOTOBJS
212 action = INTERACTIVE;
213 #else
214 action = NONE;
215 #endif
217 while (i < argc) {
218 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
219 action = LIST_IMAGES;
220 i++;
221 #ifdef WITH_BOOTOBJS
222 } else if (strcmp(argv[i],"--install")==0) {
223 action = INSTALL;
224 i++;
225 #endif
226 } else if ((strcmp(argv[i],"-d")==0) ||
227 (strcmp(argv[i],"--delete-bootloader")==0)) {
228 action = DELETE_BOOTLOADER;
229 i++;
230 } else if ((strcmp(argv[i],"-a")==0) ||
231 (strcmp(argv[i],"--add-bootloader")==0)) {
232 action = ADD_BOOTLOADER;
233 type = FILETYPE_DOT_IPOD;
234 i++;
235 if (i == argc) { print_usage(); return 1; }
236 filename=argv[i];
237 i++;
238 } else if ((strcmp(argv[i],"-ab")==0) ||
239 (strcmp(argv[i],"--add-bootloader-bin")==0)) {
240 action = ADD_BOOTLOADER;
241 type = FILETYPE_DOT_BIN;
242 i++;
243 if (i == argc) { print_usage(); return 1; }
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 type = FILETYPE_DOT_IPOD;
250 i++;
251 if (i == argc) { print_usage(); return 1; }
252 filename=argv[i];
253 i++;
254 } else if ((strcmp(argv[i],"-rfb")==0) ||
255 (strcmp(argv[i],"--read-firmware-bin")==0)) {
256 action = READ_FIRMWARE;
257 type = FILETYPE_DOT_BIN;
258 i++;
259 if (i == argc) { print_usage(); return 1; }
260 filename=argv[i];
261 i++;
262 #ifdef WITH_BOOTOBJS
263 } else if ((strcmp(argv[i],"-we")==0) ||
264 (strcmp(argv[i],"--write-embedded")==0)) {
265 action = WRITE_FIRMWARE;
266 type = FILETYPE_INTERNAL;
267 filename="[embedded bootloader]"; /* Only displayed for user */
268 i++;
269 #endif
270 } else if ((strcmp(argv[i],"-wf")==0) ||
271 (strcmp(argv[i],"--write-firmware")==0)) {
272 action = WRITE_FIRMWARE;
273 type = FILETYPE_DOT_IPOD;
274 i++;
275 if (i == argc) { print_usage(); return 1; }
276 filename=argv[i];
277 i++;
278 } else if ((strcmp(argv[i],"-wfb")==0) ||
279 (strcmp(argv[i],"--write-firmware-bin")==0)) {
280 action = WRITE_FIRMWARE;
281 type = FILETYPE_DOT_BIN;
282 i++;
283 if (i == argc) { print_usage(); return 1; }
284 filename=argv[i];
285 i++;
286 } else if ((strcmp(argv[i],"-r")==0) ||
287 (strcmp(argv[i],"--read-partition")==0)) {
288 action = READ_PARTITION;
289 i++;
290 if (i == argc) { print_usage(); return 1; }
291 filename=argv[i];
292 i++;
293 } else if ((strcmp(argv[i],"-w")==0) ||
294 (strcmp(argv[i],"--write-partition")==0)) {
295 action = WRITE_PARTITION;
296 i++;
297 if (i == argc) { print_usage(); return 1; }
298 filename=argv[i];
299 i++;
300 } else if ((strcmp(argv[i],"-v")==0) ||
301 (strcmp(argv[i],"--verbose")==0)) {
302 ipod_verbose++;
303 i++;
304 } else if ((strcmp(argv[i],"-f")==0) ||
305 (strcmp(argv[i],"--format")==0)) {
306 action = FORMAT_PARTITION;
307 i++;
308 } else if (strcmp(argv[i],"--read-aupd")==0) {
309 action = READ_AUPD;
310 i++;
311 if (i == argc) { print_usage(); return 1; }
312 filename=argv[i];
313 i++;
314 } else if (strcmp(argv[i],"--write-aupd")==0) {
315 action = WRITE_AUPD;
316 i++;
317 if (i == argc) { print_usage(); return 1; }
318 filename=argv[i];
319 i++;
320 } else if ((strcmp(argv[i],"-x")==0) ||
321 (strcmp(argv[i],"--dump-xml")==0)) {
322 action = DUMP_XML;
323 i++;
324 if (i == argc) { print_usage(); return 1; }
325 filename=argv[i];
326 i++;
327 } else if ((strcmp(argv[i],"-c")==0) ||
328 (strcmp(argv[i],"--convert")==0)) {
329 action = CONVERT_TO_FAT32;
330 i++;
331 } else {
332 print_usage(); return 1;
336 if (ipod.diskname[0]==0) {
337 print_usage();
338 return 1;
341 if (ipod_open(&ipod, 0) < 0) {
342 return 1;
345 fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname);
346 fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size);
348 if (read_partinfo(&ipod,0) < 0) {
349 return 2;
352 display_partinfo(&ipod);
354 if (ipod.pinfo[0].start==0) {
355 fprintf(stderr,"[ERR] No partition 0 on disk:\n");
356 display_partinfo(&ipod);
357 return 3;
360 read_directory(&ipod);
362 if (ipod.nimages <= 0) {
363 fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages);
364 return 1;
367 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) {
368 fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n",
369 ipod.ipod_directory[0].vers);
370 return -1;
373 #ifdef __WIN32__
374 /* Windows requires the ipod in R/W mode for SCSI Inquiry */
375 if (ipod_reopen_rw(&ipod) < 0) {
376 return 5;
378 #endif
381 /* Read the XML info, and if successful, look for the ramsize
382 (only available for some models - set to 0 if not known) */
384 ipod.ramsize = 0;
386 if (ipod_get_xmlinfo(&ipod) == 0) {
387 ipod_get_ramsize(&ipod);
390 printf("[INFO] Ipod model: %s ",ipod.modelstr);
391 if (ipod.ramsize > 0) { printf("(%dMB RAM) ",ipod.ramsize); }
392 printf("(\"%s\")\n",ipod.macpod ? "macpod" : "winpod");
394 if (ipod.ipod_directory[0].vers == 0x10000) {
395 fprintf(stderr,"[ERR] *** ipodpatcher does not support the 2nd Generation Nano! ***\n");
396 #ifdef WITH_BOOTOBJS
397 printf("Press ENTER to exit ipodpatcher :");
398 fgets(yesno,4,stdin);
399 #endif
400 return 0;
403 if (ipod.macpod) {
404 print_macpod_warning();
407 if (action==LIST_IMAGES) {
408 list_images(&ipod);
409 #ifdef WITH_BOOTOBJS
410 } else if (action==INTERACTIVE) {
412 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
414 if (fgets(yesno,4,stdin)) {
415 if (yesno[0]=='i') {
416 if (ipod_reopen_rw(&ipod) < 0) {
417 return 5;
420 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
421 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
422 } else {
423 fprintf(stderr,"[ERR] --install failed.\n");
425 } else if (yesno[0]=='u') {
426 if (ipod_reopen_rw(&ipod) < 0) {
427 return 5;
430 if (delete_bootloader(&ipod)==0) {
431 fprintf(stderr,"[INFO] Bootloader removed.\n");
432 } else {
433 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
437 #endif
438 } else if (action==DELETE_BOOTLOADER) {
439 if (ipod_reopen_rw(&ipod) < 0) {
440 return 5;
443 if (ipod.ipod_directory[0].entryOffset==0) {
444 fprintf(stderr,"[ERR] No bootloader detected.\n");
445 } else {
446 if (delete_bootloader(&ipod)==0) {
447 fprintf(stderr,"[INFO] Bootloader removed.\n");
448 } else {
449 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
452 } else if (action==ADD_BOOTLOADER) {
453 if (ipod_reopen_rw(&ipod) < 0) {
454 return 5;
457 if (add_bootloader(&ipod, filename, type)==0) {
458 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
459 } else {
460 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
462 #ifdef WITH_BOOTOBJS
463 } else if (action==INSTALL) {
464 if (ipod_reopen_rw(&ipod) < 0) {
465 return 5;
468 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
469 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
470 } else {
471 fprintf(stderr,"[ERR] --install failed.\n");
473 #endif
474 } else if (action==WRITE_FIRMWARE) {
475 if (ipod_reopen_rw(&ipod) < 0) {
476 return 5;
479 if (write_firmware(&ipod, filename,type)==0) {
480 fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename);
481 } else {
482 fprintf(stderr,"[ERR] --write-firmware failed.\n");
484 } else if (action==READ_FIRMWARE) {
485 if (read_firmware(&ipod, filename, type)==0) {
486 fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename);
487 } else {
488 fprintf(stderr,"[ERR] --read-firmware failed.\n");
490 } else if (action==READ_AUPD) {
491 if (read_aupd(&ipod, filename)==0) {
492 fprintf(stderr,"[INFO] AUPD image read to file %s.\n",filename);
493 } else {
494 fprintf(stderr,"[ERR] --read-aupd failed.\n");
496 } else if (action==WRITE_AUPD) {
497 if (ipod_reopen_rw(&ipod) < 0) {
498 return 5;
501 if (write_aupd(&ipod, filename)==0) {
502 fprintf(stderr,"[INFO] AUPD image %s written to device.\n",filename);
503 } else {
504 fprintf(stderr,"[ERR] --write-aupd failed.\n");
506 } else if (action==DUMP_XML) {
507 if (ipod.xmlinfo == NULL) {
508 fprintf(stderr,"[ERR] No XML to write\n");
509 return 1;
512 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
513 if (outfile < 0) {
514 perror(filename);
515 return 4;
518 if (write(outfile, ipod.xmlinfo, ipod.xmlinfo_len) < 0) {
519 fprintf(stderr,"[ERR] --dump-xml failed.\n");
520 } else {
521 fprintf(stderr,"[INFO] XML info written to %s.\n",filename);
523 close(outfile);
524 } else if (action==READ_PARTITION) {
525 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
526 if (outfile < 0) {
527 perror(filename);
528 return 4;
531 if (read_partition(&ipod, outfile) < 0) {
532 fprintf(stderr,"[ERR] --read-partition failed.\n");
533 } else {
534 fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename);
536 close(outfile);
537 } else if (action==WRITE_PARTITION) {
538 if (ipod_reopen_rw(&ipod) < 0) {
539 return 5;
542 infile = open(filename,O_RDONLY|O_BINARY);
543 if (infile < 0) {
544 perror(filename);
545 return 2;
548 /* Check filesize is <= partition size */
549 inputsize=filesize(infile);
550 if (inputsize > 0) {
551 if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) {
552 fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize);
553 if (write_partition(&ipod,infile) < 0) {
554 fprintf(stderr,"[ERR] --write-partition failed.\n");
555 } else {
556 fprintf(stderr,"[INFO] %s restored to partition\n",filename);
558 } else {
559 fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n");
563 close(infile);
564 } else if (action==FORMAT_PARTITION) {
565 printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n");
566 printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n");
567 printf("Are you sure you want to format your ipod? (y/n):");
569 if (fgets(yesno,4,stdin)) {
570 if (yesno[0]=='y') {
571 if (ipod_reopen_rw(&ipod) < 0) {
572 return 5;
575 if (format_partition(&ipod,1) < 0) {
576 fprintf(stderr,"[ERR] Format failed.\n");
578 } else {
579 fprintf(stderr,"[INFO] Format cancelled.\n");
582 } else if (action==CONVERT_TO_FAT32) {
583 if (!ipod.macpod) {
584 printf("[ERR] Ipod is already FAT32, aborting\n");
585 } else {
586 printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n");
587 printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n");
588 printf("Are you sure you want to convert your ipod to FAT32? (y/n):");
590 if (fgets(yesno,4,stdin)) {
591 if (yesno[0]=='y') {
592 if (ipod_reopen_rw(&ipod) < 0) {
593 return 5;
596 if (write_dos_partition_table(&ipod) < 0) {
597 fprintf(stderr,"[ERR] Partition conversion failed.\n");
600 if (format_partition(&ipod,1) < 0) {
601 fprintf(stderr,"[ERR] Format failed.\n");
603 } else {
604 fprintf(stderr,"[INFO] Format cancelled.\n");
610 ipod_close(&ipod);
612 #ifdef WITH_BOOTOBJS
613 if (action==INTERACTIVE) {
614 printf("Press ENTER to exit ipodpatcher :");
615 fgets(yesno,4,stdin);
617 #endif
619 return 0;