Index: main.c =================================================================== --- main.c (revision 16877) +++ main.c (working copy) @@ -35,12 +35,11 @@ enum { NONE, + LIST_IMAGES, #ifdef WITH_BOOTOBJS INSTALL, + INTERACTIVE, #endif - INTERACTIVE, - SHOW_INFO, - LIST_IMAGES, DELETE_BOOTLOADER, ADD_BOOTLOADER, READ_FIRMWARE, @@ -142,7 +141,7 @@ int infile, outfile; unsigned int inputsize; char* filename; - int action = SHOW_INFO; + int action = NONE; int type; struct ipod_t ipod; @@ -152,17 +151,21 @@ if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) { print_usage(); - return 1; + return 0; } if (ipod_alloc_buffer(§orbuf,BUFFER_SIZE) < 0) { fprintf(stderr,"Failed to allocate memory buffer\n"); + return 1; } if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) { - if (ipod_scan(&ipod) == 0) + if (ipod_scan(&ipod) == 0) { fprintf(stderr,"[ERR] No ipods found.\n"); - return 0; + return -1; + } else { + return 0; + } } /* If the first parameter doesn't start with -, then we interpret it as a device */ @@ -200,7 +203,7 @@ fgets(yesno,4,stdin); } #endif - return 0; + return -1; } i = 1; @@ -330,14 +333,14 @@ } if (ipod_open(&ipod, 0) < 0) { - return 1; + return -1; } fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname); fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size); if (read_partinfo(&ipod,0) < 0) { - return 2; + return -1; } display_partinfo(&ipod); @@ -345,20 +348,20 @@ if (ipod.pinfo[0].start==0) { fprintf(stderr,"[ERR] No partition 0 on disk:\n"); display_partinfo(&ipod); - return 3; + return -1; } read_directory(&ipod); if (ipod.nimages <= 0) { fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages); - return 1; + return -1; } if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) { fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n", ipod.ipod_directory[0].vers); - return -1; + return 1; } printf("[INFO] Ipod model: %s (\"%s\")\n",ipod.modelstr, @@ -370,134 +373,130 @@ printf("Press ENTER to exit ipodpatcher :"); fgets(yesno,4,stdin); #endif - return 0; + return 1; } if (ipod.macpod) { print_macpod_warning(); } - - if (action==LIST_IMAGES) { + + /* Execute the action */ + if (action > LIST_IMAGES) { + if (ipod_reopen_rw(&ipod) < 0) { + return -1; + } + } + switch (action) { + case LIST_IMAGES: list_images(&ipod); + break; #ifdef WITH_BOOTOBJS - } else if (action==INTERACTIVE) { + case INTERACTIVE: printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :"); if (fgets(yesno,4,stdin)) { if (yesno[0]=='i') { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); } else { fprintf(stderr,"[ERR] --install failed.\n"); + return 1; } } else if (yesno[0]=='u') { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - if (delete_bootloader(&ipod)==0) { fprintf(stderr,"[INFO] Bootloader removed.\n"); } else { fprintf(stderr,"[ERR] Bootloader removal failed.\n"); + return 1; } } } + break; #endif - } else if (action==DELETE_BOOTLOADER) { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - + case DELETE_BOOTLOADER: if (ipod.ipod_directory[0].entryOffset==0) { fprintf(stderr,"[ERR] No bootloader detected.\n"); + return 1; } else { if (delete_bootloader(&ipod)==0) { fprintf(stderr,"[INFO] Bootloader removed.\n"); } else { fprintf(stderr,"[ERR] --delete-bootloader failed.\n"); + return 1; } } - } else if (action==ADD_BOOTLOADER) { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - + break; + case ADD_BOOTLOADER: if (add_bootloader(&ipod, filename, type)==0) { fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename); } else { fprintf(stderr,"[ERR] --add-bootloader failed.\n"); + return 1; } + break; #ifdef WITH_BOOTOBJS - } else if (action==INSTALL) { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - + case INSTALL: if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); } else { fprintf(stderr,"[ERR] --install failed.\n"); + return 1; } + break; #endif - } else if (action==WRITE_FIRMWARE) { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - + case WRITE_FIRMWARE: if (write_firmware(&ipod, filename,type)==0) { fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename); } else { fprintf(stderr,"[ERR] --write-firmware failed.\n"); + return 1; } - } else if (action==READ_FIRMWARE) { + break; + case READ_FIRMWARE: if (read_firmware(&ipod, filename, type)==0) { fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename); } else { fprintf(stderr,"[ERR] --read-firmware failed.\n"); + return 1; } - } else if (action==READ_AUPD) { + break; + case READ_AUPD: if (read_aupd(&ipod, filename)==0) { fprintf(stderr,"[INFO] AUPD image read to file %s.\n",filename); } else { fprintf(stderr,"[ERR] --read-aupd failed.\n"); + return 1; } - } else if (action==WRITE_AUPD) { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - + break; + case WRITE_AUPD: if (write_aupd(&ipod, filename)==0) { fprintf(stderr,"[INFO] AUPD image %s written to device.\n",filename); } else { fprintf(stderr,"[ERR] --write-aupd failed.\n"); + return 1; } - } else if (action==READ_PARTITION) { + break; + case READ_PARTITION: outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); if (outfile < 0) { perror(filename); - return 4; + return 1; } if (read_partition(&ipod, outfile) < 0) { fprintf(stderr,"[ERR] --read-partition failed.\n"); + return 1; } else { fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename); } close(outfile); - } else if (action==WRITE_PARTITION) { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - + break; + case WRITE_PARTITION: infile = open(filename,O_RDONLY|O_BINARY); if (infile < 0) { perror(filename); - return 2; + return 1; } /* Check filesize is <= partition size */ @@ -507,36 +506,39 @@ fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize); if (write_partition(&ipod,infile) < 0) { fprintf(stderr,"[ERR] --write-partition failed.\n"); + return 1; } else { fprintf(stderr,"[INFO] %s restored to partition\n",filename); } } else { fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n"); + return 1; } } close(infile); - } else if (action==FORMAT_PARTITION) { + break; + case FORMAT_PARTITION: printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n"); printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n"); printf("Are you sure you want to format your ipod? (y/n):"); if (fgets(yesno,4,stdin)) { if (yesno[0]=='y') { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - if (format_partition(&ipod,1) < 0) { fprintf(stderr,"[ERR] Format failed.\n"); + return 1; } } else { fprintf(stderr,"[INFO] Format cancelled.\n"); + return 1; } } - } else if (action==CONVERT_TO_FAT32) { + break; + case CONVERT_TO_FAT32: if (!ipod.macpod) { printf("[ERR] Ipod is already FAT32, aborting\n"); + return 0; } else { printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n"); printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n"); @@ -544,23 +546,25 @@ if (fgets(yesno,4,stdin)) { if (yesno[0]=='y') { - if (ipod_reopen_rw(&ipod) < 0) { - return 5; - } - if (write_dos_partition_table(&ipod) < 0) { fprintf(stderr,"[ERR] Partition conversion failed.\n"); + return 1; } if (format_partition(&ipod,1) < 0) { fprintf(stderr,"[ERR] Format failed.\n"); + return 1; } } else { fprintf(stderr,"[INFO] Format cancelled.\n"); + return 1; } } } + break; } + case default: + break; ipod_close(&ipod);