Rename variables sectorbuf and verbose to avoid clashes in rbutil. Cleanup exports...
[kugel-rb.git] / rbutil / ipodpatcher / main.c
blob585f7277dd90a0e1de2838464ef9f5f4731aeff5
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 * 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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
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>
29 #include "ipodpatcher.h"
30 #include "ipodio.h"
32 #define VERSION "2.0 with v2.0 bootloaders"
34 enum {
35 NONE,
36 #ifdef WITH_BOOTOBJS
37 INSTALL,
38 #endif
39 INTERACTIVE,
40 SHOW_INFO,
41 LIST_IMAGES,
42 DELETE_BOOTLOADER,
43 ADD_BOOTLOADER,
44 READ_FIRMWARE,
45 WRITE_FIRMWARE,
46 READ_AUPD,
47 WRITE_AUPD,
48 READ_PARTITION,
49 WRITE_PARTITION,
50 FORMAT_PARTITION,
51 CONVERT_TO_FAT32
54 void print_macpod_warning(void)
56 printf("[INFO] ************************************************************************\n");
57 printf("[INFO] *** WARNING FOR ROCKBOX USERS\n");
58 printf("[INFO] *** You must convert this ipod to FAT32 format (aka a \"winpod\")\n");
59 printf("[INFO] *** if you want to run Rockbox. Rockbox WILL NOT work on this ipod.\n");
60 printf("[INFO] *** See http://www.rockbox.org/twiki/bin/view/Main/IpodConversionToFAT32\n");
61 printf("[INFO] ************************************************************************\n");
64 void print_usage(void)
66 fprintf(stderr,"Usage: ipodpatcher --scan\n");
67 #ifdef __WIN32__
68 fprintf(stderr," or ipodpatcher [DISKNO] [action]\n");
69 #else
70 fprintf(stderr," or ipodpatcher [device] [action]\n");
71 #endif
72 fprintf(stderr,"\n");
73 fprintf(stderr,"Where [action] is one of the following options:\n");
74 #ifdef WITH_BOOTOBJS
75 fprintf(stderr," --install\n");
76 #endif
77 fprintf(stderr," -l, --list\n");
78 fprintf(stderr," -r, --read-partition bootpartition.bin\n");
79 fprintf(stderr," -w, --write-partition bootpartition.bin\n");
80 fprintf(stderr," -rf, --read-firmware filename.ipod\n");
81 fprintf(stderr," -rfb, --read-firmware-bin filename.bin\n");
82 fprintf(stderr," -wf, --write-firmware filename.ipod\n");
83 fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n");
84 #ifdef WITH_BOOTOBJS
85 fprintf(stderr," -we, --write-embedded\n");
86 #endif
87 fprintf(stderr," -a, --add-bootloader filename.ipod\n");
88 fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n");
89 fprintf(stderr," -d, --delete-bootloader\n");
90 fprintf(stderr," -f, --format\n");
91 fprintf(stderr," -c, --convert\n");
92 fprintf(stderr," --read-aupd filename.bin\n");
93 fprintf(stderr," --write-aupd filename.bin\n");
94 fprintf(stderr,"\n");
96 #ifdef __WIN32__
97 fprintf(stderr,"DISKNO is the number (e.g. 2) Windows has assigned to your ipod's hard disk.\n");
98 fprintf(stderr,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
99 fprintf(stderr,"will be disk 1 etc. ipodpatcher will refuse to access a disk unless it\n");
100 fprintf(stderr,"can identify it as being an ipod.\n");
101 fprintf(stderr,"\n");
102 #else
103 #if defined(linux) || defined (__linux)
104 fprintf(stderr,"\"device\" is the device node (e.g. /dev/sda) assigned to your ipod.\n");
105 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
106 fprintf(stderr,"\"device\" is the device node (e.g. /dev/da1) assigned to your ipod.\n");
107 #elif defined(__APPLE__) && defined(__MACH__)
108 fprintf(stderr,"\"device\" is the device node (e.g. /dev/disk1) assigned to your ipod.\n");
109 #endif
110 fprintf(stderr,"ipodpatcher will refuse to access a disk unless it can identify it as being\n");
111 fprintf(stderr,"an ipod.\n");
112 #endif
115 void display_partinfo(struct ipod_t* ipod)
117 int i;
118 double sectors_per_MB = (1024.0*1024.0)/ipod->sector_size;
120 printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
121 for ( i = 0; i < 4; i++ ) {
122 if (ipod->pinfo[i].start != 0) {
123 printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
125 (long int)ipod->pinfo[i].start,
126 (long int)ipod->pinfo[i].start+ipod->pinfo[i].size-1,
127 ipod->pinfo[i].size/sectors_per_MB,
128 get_parttype(ipod->pinfo[i].type),
129 (int)ipod->pinfo[i].type);
135 int main(int argc, char* argv[])
137 char yesno[4];
138 int i;
139 int n;
140 int infile, outfile;
141 unsigned int inputsize;
142 char* filename;
143 int action = SHOW_INFO;
144 int type;
145 struct ipod_t ipod;
147 fprintf(stderr,"ipodpatcher v" VERSION " - (C) Dave Chapman 2006-2007\n");
148 fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n");
149 fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
151 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
152 print_usage();
153 return 1;
156 if (ipod_alloc_buffer(&ipod_sectorbuf,BUFFER_SIZE) < 0) {
157 fprintf(stderr,"Failed to allocate memory buffer\n");
160 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
161 if (ipod_scan(&ipod) == 0)
162 fprintf(stderr,"[ERR] No ipods found.\n");
163 return 0;
166 /* If the first parameter doesn't start with -, then we interpret it as a device */
167 if ((argc > 1) && (argv[1][0] != '-')) {
168 ipod.diskname[0]=0;
169 #ifdef __WIN32__
170 snprintf(ipod.diskname,sizeof(ipod.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
171 #else
172 strncpy(ipod.diskname,argv[1],sizeof(ipod.diskname));
173 #endif
174 i = 2;
175 } else {
176 /* Autoscan for ipods */
177 n = ipod_scan(&ipod);
178 if (n==0) {
179 fprintf(stderr,"[ERR] No ipods found, aborting\n");
180 fprintf(stderr,"[ERR] Please connect your ipod and ensure it is in disk mode\n");
181 #if defined(__APPLE__) && defined(__MACH__)
182 fprintf(stderr,"[ERR] Also ensure that itunes is closed, and that your ipod is not mounted.\n");
183 #elif !defined(__WIN32__)
184 if (geteuid()!=0) {
185 fprintf(stderr,"[ERR] You may also need to run ipodpatcher as root.\n");
187 #endif
188 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
189 } else if (n > 1) {
190 fprintf(stderr,"[ERR] %d ipods found, aborting\n",n);
191 fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\n");
194 if (n != 1) {
195 #ifdef WITH_BOOTOBJS
196 if (argc==1) {
197 printf("\nPress ENTER to exit ipodpatcher :");
198 fgets(yesno,4,stdin);
200 #endif
201 return 0;
204 i = 1;
207 #ifdef WITH_BOOTOBJS
208 action = INTERACTIVE;
209 #else
210 action = NONE;
211 #endif
213 while (i < argc) {
214 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
215 action = LIST_IMAGES;
216 i++;
217 #ifdef WITH_BOOTOBJS
218 } else if (strcmp(argv[i],"--install")==0) {
219 action = INSTALL;
220 i++;
221 #endif
222 } else if ((strcmp(argv[i],"-d")==0) ||
223 (strcmp(argv[i],"--delete-bootloader")==0)) {
224 action = DELETE_BOOTLOADER;
225 i++;
226 } else if ((strcmp(argv[i],"-a")==0) ||
227 (strcmp(argv[i],"--add-bootloader")==0)) {
228 action = ADD_BOOTLOADER;
229 type = FILETYPE_DOT_IPOD;
230 i++;
231 if (i == argc) { print_usage(); return 1; }
232 filename=argv[i];
233 i++;
234 } else if ((strcmp(argv[i],"-ab")==0) ||
235 (strcmp(argv[i],"--add-bootloader-bin")==0)) {
236 action = ADD_BOOTLOADER;
237 type = FILETYPE_DOT_BIN;
238 i++;
239 if (i == argc) { print_usage(); return 1; }
240 filename=argv[i];
241 i++;
242 } else if ((strcmp(argv[i],"-rf")==0) ||
243 (strcmp(argv[i],"--read-firmware")==0)) {
244 action = READ_FIRMWARE;
245 type = FILETYPE_DOT_IPOD;
246 i++;
247 if (i == argc) { print_usage(); return 1; }
248 filename=argv[i];
249 i++;
250 } else if ((strcmp(argv[i],"-rfb")==0) ||
251 (strcmp(argv[i],"--read-firmware-bin")==0)) {
252 action = READ_FIRMWARE;
253 type = FILETYPE_DOT_BIN;
254 i++;
255 if (i == argc) { print_usage(); return 1; }
256 filename=argv[i];
257 i++;
258 #ifdef WITH_BOOTOBJS
259 } else if ((strcmp(argv[i],"-we")==0) ||
260 (strcmp(argv[i],"--write-embedded")==0)) {
261 action = WRITE_FIRMWARE;
262 type = FILETYPE_INTERNAL;
263 filename="[embedded bootloader]"; /* Only displayed for user */
264 i++;
265 #endif
266 } else if ((strcmp(argv[i],"-wf")==0) ||
267 (strcmp(argv[i],"--write-firmware")==0)) {
268 action = WRITE_FIRMWARE;
269 type = FILETYPE_DOT_IPOD;
270 i++;
271 if (i == argc) { print_usage(); return 1; }
272 filename=argv[i];
273 i++;
274 } else if ((strcmp(argv[i],"-wfb")==0) ||
275 (strcmp(argv[i],"--write-firmware-bin")==0)) {
276 action = WRITE_FIRMWARE;
277 type = FILETYPE_DOT_BIN;
278 i++;
279 if (i == argc) { print_usage(); return 1; }
280 filename=argv[i];
281 i++;
282 } else if ((strcmp(argv[i],"-r")==0) ||
283 (strcmp(argv[i],"--read-partition")==0)) {
284 action = READ_PARTITION;
285 i++;
286 if (i == argc) { print_usage(); return 1; }
287 filename=argv[i];
288 i++;
289 } else if ((strcmp(argv[i],"-w")==0) ||
290 (strcmp(argv[i],"--write-partition")==0)) {
291 action = WRITE_PARTITION;
292 i++;
293 if (i == argc) { print_usage(); return 1; }
294 filename=argv[i];
295 i++;
296 } else if ((strcmp(argv[i],"-v")==0) ||
297 (strcmp(argv[i],"--verbose")==0)) {
298 ipod_verbose++;
299 i++;
300 } else if ((strcmp(argv[i],"-f")==0) ||
301 (strcmp(argv[i],"--format")==0)) {
302 action = FORMAT_PARTITION;
303 i++;
304 } else if (strcmp(argv[i],"--read-aupd")==0) {
305 action = READ_AUPD;
306 i++;
307 if (i == argc) { print_usage(); return 1; }
308 filename=argv[i];
309 i++;
310 } else if (strcmp(argv[i],"--write-aupd")==0) {
311 action = WRITE_AUPD;
312 i++;
313 if (i == argc) { print_usage(); return 1; }
314 filename=argv[i];
315 i++;
316 } else if ((strcmp(argv[i],"-c")==0) ||
317 (strcmp(argv[i],"--convert")==0)) {
318 action = CONVERT_TO_FAT32;
319 i++;
320 } else {
321 print_usage(); return 1;
325 if (ipod.diskname[0]==0) {
326 print_usage();
327 return 1;
330 if (ipod_open(&ipod, 0) < 0) {
331 return 1;
334 fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname);
335 fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size);
337 if (read_partinfo(&ipod,0) < 0) {
338 return 2;
341 display_partinfo(&ipod);
343 if (ipod.pinfo[0].start==0) {
344 fprintf(stderr,"[ERR] No partition 0 on disk:\n");
345 display_partinfo(&ipod);
346 return 3;
349 read_directory(&ipod);
351 if (ipod.nimages <= 0) {
352 fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages);
353 return 1;
356 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) {
357 fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n",
358 ipod.ipod_directory[0].vers);
359 return -1;
362 printf("[INFO] Ipod model: %s (\"%s\")\n",ipod.modelstr,
363 ipod.macpod ? "macpod" : "winpod");
365 if (ipod.ipod_directory[0].vers == 0x10000) {
366 fprintf(stderr,"[ERR] *** ipodpatcher does not support the 2nd Generation Nano! ***\n");
367 #ifdef WITH_BOOTOBJS
368 printf("Press ENTER to exit ipodpatcher :");
369 fgets(yesno,4,stdin);
370 #endif
371 return 0;
374 if (ipod.macpod) {
375 print_macpod_warning();
378 if (action==LIST_IMAGES) {
379 list_images(&ipod);
380 #ifdef WITH_BOOTOBJS
381 } else if (action==INTERACTIVE) {
383 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
385 if (fgets(yesno,4,stdin)) {
386 if (yesno[0]=='i') {
387 if (ipod_reopen_rw(&ipod) < 0) {
388 return 5;
391 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
392 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
393 } else {
394 fprintf(stderr,"[ERR] --install failed.\n");
396 } else if (yesno[0]=='u') {
397 if (ipod_reopen_rw(&ipod) < 0) {
398 return 5;
401 if (delete_bootloader(&ipod)==0) {
402 fprintf(stderr,"[INFO] Bootloader removed.\n");
403 } else {
404 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
408 #endif
409 } else if (action==DELETE_BOOTLOADER) {
410 if (ipod_reopen_rw(&ipod) < 0) {
411 return 5;
414 if (ipod.ipod_directory[0].entryOffset==0) {
415 fprintf(stderr,"[ERR] No bootloader detected.\n");
416 } else {
417 if (delete_bootloader(&ipod)==0) {
418 fprintf(stderr,"[INFO] Bootloader removed.\n");
419 } else {
420 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
423 } else if (action==ADD_BOOTLOADER) {
424 if (ipod_reopen_rw(&ipod) < 0) {
425 return 5;
428 if (add_bootloader(&ipod, filename, type)==0) {
429 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
430 } else {
431 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
433 #ifdef WITH_BOOTOBJS
434 } else if (action==INSTALL) {
435 if (ipod_reopen_rw(&ipod) < 0) {
436 return 5;
439 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
440 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
441 } else {
442 fprintf(stderr,"[ERR] --install failed.\n");
444 #endif
445 } else if (action==WRITE_FIRMWARE) {
446 if (ipod_reopen_rw(&ipod) < 0) {
447 return 5;
450 if (write_firmware(&ipod, filename,type)==0) {
451 fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename);
452 } else {
453 fprintf(stderr,"[ERR] --write-firmware failed.\n");
455 } else if (action==READ_FIRMWARE) {
456 if (read_firmware(&ipod, filename, type)==0) {
457 fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename);
458 } else {
459 fprintf(stderr,"[ERR] --read-firmware failed.\n");
461 } else if (action==READ_AUPD) {
462 if (read_aupd(&ipod, filename)==0) {
463 fprintf(stderr,"[INFO] AUPD image read to file %s.\n",filename);
464 } else {
465 fprintf(stderr,"[ERR] --read-aupd failed.\n");
467 } else if (action==WRITE_AUPD) {
468 if (ipod_reopen_rw(&ipod) < 0) {
469 return 5;
472 if (write_aupd(&ipod, filename)==0) {
473 fprintf(stderr,"[INFO] AUPD image %s written to device.\n",filename);
474 } else {
475 fprintf(stderr,"[ERR] --write-aupd failed.\n");
477 } else if (action==READ_PARTITION) {
478 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
479 if (outfile < 0) {
480 perror(filename);
481 return 4;
484 if (read_partition(&ipod, outfile) < 0) {
485 fprintf(stderr,"[ERR] --read-partition failed.\n");
486 } else {
487 fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename);
489 close(outfile);
490 } else if (action==WRITE_PARTITION) {
491 if (ipod_reopen_rw(&ipod) < 0) {
492 return 5;
495 infile = open(filename,O_RDONLY|O_BINARY);
496 if (infile < 0) {
497 perror(filename);
498 return 2;
501 /* Check filesize is <= partition size */
502 inputsize=filesize(infile);
503 if (inputsize > 0) {
504 if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) {
505 fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize);
506 if (write_partition(&ipod,infile) < 0) {
507 fprintf(stderr,"[ERR] --write-partition failed.\n");
508 } else {
509 fprintf(stderr,"[INFO] %s restored to partition\n",filename);
511 } else {
512 fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n");
516 close(infile);
517 } else if (action==FORMAT_PARTITION) {
518 printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n");
519 printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n");
520 printf("Are you sure you want to format your ipod? (y/n):");
522 if (fgets(yesno,4,stdin)) {
523 if (yesno[0]=='y') {
524 if (ipod_reopen_rw(&ipod) < 0) {
525 return 5;
528 if (format_partition(&ipod,1) < 0) {
529 fprintf(stderr,"[ERR] Format failed.\n");
531 } else {
532 fprintf(stderr,"[INFO] Format cancelled.\n");
535 } else if (action==CONVERT_TO_FAT32) {
536 if (!ipod.macpod) {
537 printf("[ERR] Ipod is already FAT32, aborting\n");
538 } else {
539 printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n");
540 printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n");
541 printf("Are you sure you want to convert your ipod to FAT32? (y/n):");
543 if (fgets(yesno,4,stdin)) {
544 if (yesno[0]=='y') {
545 if (ipod_reopen_rw(&ipod) < 0) {
546 return 5;
549 if (write_dos_partition_table(&ipod) < 0) {
550 fprintf(stderr,"[ERR] Partition conversion failed.\n");
553 if (format_partition(&ipod,1) < 0) {
554 fprintf(stderr,"[ERR] Format failed.\n");
556 } else {
557 fprintf(stderr,"[INFO] Format cancelled.\n");
563 ipod_close(&ipod);
565 #ifdef WITH_BOOTOBJS
566 if (action==INTERACTIVE) {
567 printf("Press ENTER to exit ipodpatcher :");
568 fgets(yesno,4,stdin);
570 #endif
573 return 0;