Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id: ipodpatcher.c 12237 2007-02-08 21:31:38Z dave $ |
| 9 | * |
| 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 "ipodpatcher.h" |
| 30 | #include "ipodio.h" |
| 31 | |
Dave Chapman | 36427dd | 2007-09-18 19:24:08 +0000 | [diff] [blame] | 32 | #define VERSION "2.0 with v2.0 bootloaders" |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 33 | |
| 34 | int verbose = 0; |
| 35 | |
| 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, |
Dave Chapman | 6e79715 | 2007-09-08 23:27:49 +0000 | [diff] [blame] | 48 | READ_AUPD, |
| 49 | WRITE_AUPD, |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 50 | READ_PARTITION, |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 51 | WRITE_PARTITION, |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 52 | FORMAT_PARTITION, |
| 53 | CONVERT_TO_FAT32 |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | void print_macpod_warning(void) |
| 57 | { |
| 58 | printf("[INFO] ************************************************************************\n"); |
| 59 | printf("[INFO] *** WARNING FOR ROCKBOX USERS\n"); |
| 60 | printf("[INFO] *** You must convert this ipod to FAT32 format (aka a \"winpod\")\n"); |
| 61 | printf("[INFO] *** if you want to run Rockbox. Rockbox WILL NOT work on this ipod.\n"); |
| 62 | printf("[INFO] *** See http://www.rockbox.org/twiki/bin/view/Main/IpodConversionToFAT32\n"); |
| 63 | printf("[INFO] ************************************************************************\n"); |
| 64 | } |
| 65 | |
| 66 | void print_usage(void) |
| 67 | { |
| 68 | fprintf(stderr,"Usage: ipodpatcher --scan\n"); |
| 69 | #ifdef __WIN32__ |
| 70 | fprintf(stderr," or ipodpatcher [DISKNO] [action]\n"); |
| 71 | #else |
| 72 | fprintf(stderr," or ipodpatcher [device] [action]\n"); |
| 73 | #endif |
| 74 | fprintf(stderr,"\n"); |
| 75 | fprintf(stderr,"Where [action] is one of the following options:\n"); |
| 76 | #ifdef WITH_BOOTOBJS |
| 77 | fprintf(stderr," --install\n"); |
| 78 | #endif |
| 79 | fprintf(stderr," -l, --list\n"); |
| 80 | fprintf(stderr," -r, --read-partition bootpartition.bin\n"); |
| 81 | fprintf(stderr," -w, --write-partition bootpartition.bin\n"); |
| 82 | fprintf(stderr," -rf, --read-firmware filename.ipod\n"); |
Dave Chapman | 427fff4 | 2007-04-13 23:28:20 +0000 | [diff] [blame] | 83 | fprintf(stderr," -rfb, --read-firmware-bin filename.bin\n"); |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 84 | fprintf(stderr," -wf, --write-firmware filename.ipod\n"); |
| 85 | fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n"); |
Dave Chapman | bbde445 | 2007-06-02 10:10:31 +0000 | [diff] [blame] | 86 | #ifdef WITH_BOOTOBJS |
| 87 | fprintf(stderr," -we, --write-embedded\n"); |
| 88 | #endif |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 89 | fprintf(stderr," -a, --add-bootloader filename.ipod\n"); |
| 90 | fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n"); |
| 91 | fprintf(stderr," -d, --delete-bootloader\n"); |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 92 | fprintf(stderr," -f, --format\n"); |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 93 | fprintf(stderr," -c, --convert\n"); |
Dave Chapman | 6e79715 | 2007-09-08 23:27:49 +0000 | [diff] [blame] | 94 | fprintf(stderr," --read-aupd filename.bin\n"); |
| 95 | fprintf(stderr," --write-aupd filename.bin\n"); |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 96 | fprintf(stderr,"\n"); |
| 97 | |
| 98 | #ifdef __WIN32__ |
| 99 | fprintf(stderr,"DISKNO is the number (e.g. 2) Windows has assigned to your ipod's hard disk.\n"); |
| 100 | fprintf(stderr,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n"); |
| 101 | fprintf(stderr,"will be disk 1 etc. ipodpatcher will refuse to access a disk unless it\n"); |
| 102 | fprintf(stderr,"can identify it as being an ipod.\n"); |
| 103 | fprintf(stderr,"\n"); |
| 104 | #else |
| 105 | #if defined(linux) || defined (__linux) |
| 106 | fprintf(stderr,"\"device\" is the device node (e.g. /dev/sda) assigned to your ipod.\n"); |
| 107 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) |
| 108 | fprintf(stderr,"\"device\" is the device node (e.g. /dev/da1) assigned to your ipod.\n"); |
| 109 | #elif defined(__APPLE__) && defined(__MACH__) |
| 110 | fprintf(stderr,"\"device\" is the device node (e.g. /dev/disk1) assigned to your ipod.\n"); |
| 111 | #endif |
| 112 | fprintf(stderr,"ipodpatcher will refuse to access a disk unless it can identify it as being\n"); |
| 113 | fprintf(stderr,"an ipod.\n"); |
| 114 | #endif |
| 115 | } |
| 116 | |
Dave Chapman | a6d68bd | 2007-02-10 20:09:23 +0000 | [diff] [blame] | 117 | void display_partinfo(struct ipod_t* ipod) |
| 118 | { |
| 119 | int i; |
| 120 | double sectors_per_MB = (1024.0*1024.0)/ipod->sector_size; |
| 121 | |
| 122 | printf("[INFO] Part Start Sector End Sector Size (MB) Type\n"); |
| 123 | for ( i = 0; i < 4; i++ ) { |
| 124 | if (ipod->pinfo[i].start != 0) { |
| 125 | printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n", |
| 126 | i, |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 127 | (long int)ipod->pinfo[i].start, |
| 128 | (long int)ipod->pinfo[i].start+ipod->pinfo[i].size-1, |
Dave Chapman | a6d68bd | 2007-02-10 20:09:23 +0000 | [diff] [blame] | 129 | ipod->pinfo[i].size/sectors_per_MB, |
| 130 | get_parttype(ipod->pinfo[i].type), |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 131 | (int)ipod->pinfo[i].type); |
Dave Chapman | a6d68bd | 2007-02-10 20:09:23 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 137 | int main(int argc, char* argv[]) |
| 138 | { |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 139 | char yesno[4]; |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 140 | int i; |
| 141 | int n; |
| 142 | int infile, outfile; |
| 143 | unsigned int inputsize; |
| 144 | char* filename; |
| 145 | int action = SHOW_INFO; |
| 146 | int type; |
| 147 | struct ipod_t ipod; |
| 148 | |
| 149 | fprintf(stderr,"ipodpatcher 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 1; |
| 156 | } |
| 157 | |
| 158 | if (ipod_alloc_buffer(§orbuf,BUFFER_SIZE) < 0) { |
| 159 | fprintf(stderr,"Failed to allocate memory buffer\n"); |
| 160 | } |
| 161 | |
| 162 | if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) { |
| 163 | if (ipod_scan(&ipod) == 0) |
| 164 | fprintf(stderr,"[ERR] No ipods found.\n"); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* If the first parameter doesn't start with -, then we interpret it as a device */ |
| 169 | if ((argc > 1) && (argv[1][0] != '-')) { |
| 170 | ipod.diskname[0]=0; |
| 171 | #ifdef __WIN32__ |
| 172 | snprintf(ipod.diskname,sizeof(ipod.diskname),"\\\\.\\PhysicalDrive%s",argv[1]); |
| 173 | #else |
| 174 | strncpy(ipod.diskname,argv[1],sizeof(ipod.diskname)); |
| 175 | #endif |
| 176 | i = 2; |
| 177 | } else { |
| 178 | /* Autoscan for ipods */ |
| 179 | n = ipod_scan(&ipod); |
| 180 | if (n==0) { |
| 181 | fprintf(stderr,"[ERR] No ipods found, aborting\n"); |
| 182 | fprintf(stderr,"[ERR] Please connect your ipod and ensure it is in disk mode\n"); |
Dave Chapman | 4642430 | 2007-02-09 09:45:29 +0000 | [diff] [blame] | 183 | #if defined(__APPLE__) && defined(__MACH__) |
| 184 | fprintf(stderr,"[ERR] Also ensure that itunes is closed, and that your ipod is not mounted.\n"); |
| 185 | #elif !defined(__WIN32__) |
| 186 | if (geteuid()!=0) { |
| 187 | fprintf(stderr,"[ERR] You may also need to run ipodpatcher as root.\n"); |
| 188 | } |
| 189 | #endif |
| 190 | fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n"); |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 191 | } else if (n > 1) { |
| 192 | fprintf(stderr,"[ERR] %d ipods found, aborting\n",n); |
Dave Chapman | 4642430 | 2007-02-09 09:45:29 +0000 | [diff] [blame] | 193 | fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\n"); |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if (n != 1) { |
| 197 | #ifdef WITH_BOOTOBJS |
| 198 | if (argc==1) { |
| 199 | printf("\nPress ENTER to exit ipodpatcher :"); |
| 200 | fgets(yesno,4,stdin); |
| 201 | } |
| 202 | #endif |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | i = 1; |
| 207 | } |
| 208 | |
| 209 | #ifdef WITH_BOOTOBJS |
| 210 | action = INTERACTIVE; |
| 211 | #else |
| 212 | action = NONE; |
| 213 | #endif |
| 214 | |
| 215 | while (i < argc) { |
| 216 | if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) { |
| 217 | action = LIST_IMAGES; |
| 218 | i++; |
| 219 | #ifdef WITH_BOOTOBJS |
| 220 | } else if (strcmp(argv[i],"--install")==0) { |
| 221 | action = INSTALL; |
| 222 | i++; |
| 223 | #endif |
| 224 | } else if ((strcmp(argv[i],"-d")==0) || |
| 225 | (strcmp(argv[i],"--delete-bootloader")==0)) { |
| 226 | action = DELETE_BOOTLOADER; |
| 227 | i++; |
| 228 | } else if ((strcmp(argv[i],"-a")==0) || |
| 229 | (strcmp(argv[i],"--add-bootloader")==0)) { |
| 230 | action = ADD_BOOTLOADER; |
| 231 | type = FILETYPE_DOT_IPOD; |
| 232 | i++; |
| 233 | if (i == argc) { print_usage(); return 1; } |
| 234 | filename=argv[i]; |
| 235 | i++; |
| 236 | } else if ((strcmp(argv[i],"-ab")==0) || |
| 237 | (strcmp(argv[i],"--add-bootloader-bin")==0)) { |
| 238 | action = ADD_BOOTLOADER; |
| 239 | type = FILETYPE_DOT_BIN; |
| 240 | i++; |
| 241 | if (i == argc) { print_usage(); return 1; } |
| 242 | filename=argv[i]; |
| 243 | i++; |
| 244 | } else if ((strcmp(argv[i],"-rf")==0) || |
| 245 | (strcmp(argv[i],"--read-firmware")==0)) { |
| 246 | action = READ_FIRMWARE; |
Dave Chapman | 427fff4 | 2007-04-13 23:28:20 +0000 | [diff] [blame] | 247 | type = FILETYPE_DOT_IPOD; |
| 248 | i++; |
| 249 | if (i == argc) { print_usage(); return 1; } |
| 250 | filename=argv[i]; |
| 251 | i++; |
| 252 | } else if ((strcmp(argv[i],"-rfb")==0) || |
| 253 | (strcmp(argv[i],"--read-firmware-bin")==0)) { |
| 254 | action = READ_FIRMWARE; |
| 255 | type = FILETYPE_DOT_BIN; |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 256 | i++; |
| 257 | if (i == argc) { print_usage(); return 1; } |
| 258 | filename=argv[i]; |
| 259 | i++; |
Dave Chapman | bbde445 | 2007-06-02 10:10:31 +0000 | [diff] [blame] | 260 | #ifdef WITH_BOOTOBJS |
| 261 | } else if ((strcmp(argv[i],"-we")==0) || |
| 262 | (strcmp(argv[i],"--write-embedded")==0)) { |
| 263 | action = WRITE_FIRMWARE; |
| 264 | type = FILETYPE_INTERNAL; |
| 265 | filename="[embedded bootloader]"; /* Only displayed for user */ |
| 266 | i++; |
| 267 | #endif |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 268 | } else if ((strcmp(argv[i],"-wf")==0) || |
| 269 | (strcmp(argv[i],"--write-firmware")==0)) { |
| 270 | action = WRITE_FIRMWARE; |
| 271 | type = FILETYPE_DOT_IPOD; |
| 272 | i++; |
| 273 | if (i == argc) { print_usage(); return 1; } |
| 274 | filename=argv[i]; |
| 275 | i++; |
| 276 | } else if ((strcmp(argv[i],"-wfb")==0) || |
| 277 | (strcmp(argv[i],"--write-firmware-bin")==0)) { |
| 278 | action = WRITE_FIRMWARE; |
| 279 | type = FILETYPE_DOT_BIN; |
| 280 | i++; |
| 281 | if (i == argc) { print_usage(); return 1; } |
| 282 | filename=argv[i]; |
| 283 | i++; |
| 284 | } else if ((strcmp(argv[i],"-r")==0) || |
| 285 | (strcmp(argv[i],"--read-partition")==0)) { |
| 286 | action = READ_PARTITION; |
| 287 | i++; |
| 288 | if (i == argc) { print_usage(); return 1; } |
| 289 | filename=argv[i]; |
| 290 | i++; |
| 291 | } else if ((strcmp(argv[i],"-w")==0) || |
| 292 | (strcmp(argv[i],"--write-partition")==0)) { |
| 293 | action = WRITE_PARTITION; |
| 294 | i++; |
| 295 | if (i == argc) { print_usage(); return 1; } |
| 296 | filename=argv[i]; |
| 297 | i++; |
| 298 | } else if ((strcmp(argv[i],"-v")==0) || |
| 299 | (strcmp(argv[i],"--verbose")==0)) { |
| 300 | verbose++; |
| 301 | i++; |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 302 | } else if ((strcmp(argv[i],"-f")==0) || |
| 303 | (strcmp(argv[i],"--format")==0)) { |
| 304 | action = FORMAT_PARTITION; |
| 305 | i++; |
Dave Chapman | 6e79715 | 2007-09-08 23:27:49 +0000 | [diff] [blame] | 306 | } else if (strcmp(argv[i],"--read-aupd")==0) { |
| 307 | action = READ_AUPD; |
| 308 | i++; |
| 309 | if (i == argc) { print_usage(); return 1; } |
| 310 | filename=argv[i]; |
| 311 | i++; |
| 312 | } else if (strcmp(argv[i],"--write-aupd")==0) { |
| 313 | action = WRITE_AUPD; |
| 314 | i++; |
| 315 | if (i == argc) { print_usage(); return 1; } |
| 316 | filename=argv[i]; |
| 317 | i++; |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 318 | } else if ((strcmp(argv[i],"-c")==0) || |
| 319 | (strcmp(argv[i],"--convert")==0)) { |
| 320 | action = CONVERT_TO_FAT32; |
| 321 | i++; |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 322 | } else { |
| 323 | print_usage(); return 1; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (ipod.diskname[0]==0) { |
| 328 | print_usage(); |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | if (ipod_open(&ipod, 0) < 0) { |
| 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname); |
| 337 | fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size); |
| 338 | |
| 339 | if (read_partinfo(&ipod,0) < 0) { |
| 340 | return 2; |
| 341 | } |
| 342 | |
| 343 | display_partinfo(&ipod); |
| 344 | |
| 345 | if (ipod.pinfo[0].start==0) { |
| 346 | fprintf(stderr,"[ERR] No partition 0 on disk:\n"); |
| 347 | display_partinfo(&ipod); |
| 348 | return 3; |
| 349 | } |
| 350 | |
| 351 | read_directory(&ipod); |
| 352 | |
| 353 | if (ipod.nimages <= 0) { |
| 354 | fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages); |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) { |
| 359 | fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n", |
| 360 | ipod.ipod_directory[0].vers); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | printf("[INFO] Ipod model: %s (\"%s\")\n",ipod.modelstr, |
| 365 | ipod.macpod ? "macpod" : "winpod"); |
| 366 | |
Dave Chapman | 564d249 | 2007-08-03 02:12:32 +0000 | [diff] [blame] | 367 | if (ipod.ipod_directory[0].vers == 0x10000) { |
| 368 | fprintf(stderr,"[ERR] *** ipodpatcher does not support the 2nd Generation Nano! ***\n"); |
| 369 | #ifdef WITH_BOOTOBJS |
| 370 | printf("Press ENTER to exit ipodpatcher :"); |
| 371 | fgets(yesno,4,stdin); |
| 372 | #endif |
| 373 | return 0; |
| 374 | } |
| 375 | |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 376 | if (ipod.macpod) { |
| 377 | print_macpod_warning(); |
| 378 | } |
| 379 | |
| 380 | if (action==LIST_IMAGES) { |
| 381 | list_images(&ipod); |
| 382 | #ifdef WITH_BOOTOBJS |
| 383 | } else if (action==INTERACTIVE) { |
| 384 | |
Dave Chapman | 9b4bb47 | 2007-03-06 14:07:38 +0000 | [diff] [blame] | 385 | printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :"); |
| 386 | |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 387 | if (fgets(yesno,4,stdin)) { |
Dave Chapman | 9b4bb47 | 2007-03-06 14:07:38 +0000 | [diff] [blame] | 388 | if (yesno[0]=='i') { |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 389 | if (ipod_reopen_rw(&ipod) < 0) { |
| 390 | return 5; |
| 391 | } |
| 392 | |
| 393 | if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { |
| 394 | fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); |
| 395 | } else { |
| 396 | fprintf(stderr,"[ERR] --install failed.\n"); |
| 397 | } |
Dave Chapman | 9b4bb47 | 2007-03-06 14:07:38 +0000 | [diff] [blame] | 398 | } else if (yesno[0]=='u') { |
| 399 | if (ipod_reopen_rw(&ipod) < 0) { |
| 400 | return 5; |
| 401 | } |
| 402 | |
| 403 | if (delete_bootloader(&ipod)==0) { |
| 404 | fprintf(stderr,"[INFO] Bootloader removed.\n"); |
| 405 | } else { |
| 406 | fprintf(stderr,"[ERR] Bootloader removal failed.\n"); |
| 407 | } |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | #endif |
| 411 | } else if (action==DELETE_BOOTLOADER) { |
| 412 | if (ipod_reopen_rw(&ipod) < 0) { |
| 413 | return 5; |
| 414 | } |
| 415 | |
| 416 | if (ipod.ipod_directory[0].entryOffset==0) { |
| 417 | fprintf(stderr,"[ERR] No bootloader detected.\n"); |
| 418 | } else { |
| 419 | if (delete_bootloader(&ipod)==0) { |
| 420 | fprintf(stderr,"[INFO] Bootloader removed.\n"); |
| 421 | } else { |
| 422 | fprintf(stderr,"[ERR] --delete-bootloader failed.\n"); |
| 423 | } |
| 424 | } |
| 425 | } else if (action==ADD_BOOTLOADER) { |
| 426 | if (ipod_reopen_rw(&ipod) < 0) { |
| 427 | return 5; |
| 428 | } |
| 429 | |
| 430 | if (add_bootloader(&ipod, filename, type)==0) { |
| 431 | fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename); |
| 432 | } else { |
| 433 | fprintf(stderr,"[ERR] --add-bootloader failed.\n"); |
| 434 | } |
| 435 | #ifdef WITH_BOOTOBJS |
| 436 | } else if (action==INSTALL) { |
| 437 | if (ipod_reopen_rw(&ipod) < 0) { |
| 438 | return 5; |
| 439 | } |
| 440 | |
| 441 | if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { |
| 442 | fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); |
| 443 | } else { |
| 444 | fprintf(stderr,"[ERR] --install failed.\n"); |
| 445 | } |
| 446 | #endif |
| 447 | } else if (action==WRITE_FIRMWARE) { |
| 448 | if (ipod_reopen_rw(&ipod) < 0) { |
| 449 | return 5; |
| 450 | } |
| 451 | |
| 452 | if (write_firmware(&ipod, filename,type)==0) { |
| 453 | fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename); |
| 454 | } else { |
| 455 | fprintf(stderr,"[ERR] --write-firmware failed.\n"); |
| 456 | } |
| 457 | } else if (action==READ_FIRMWARE) { |
Dave Chapman | 427fff4 | 2007-04-13 23:28:20 +0000 | [diff] [blame] | 458 | if (read_firmware(&ipod, filename, type)==0) { |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 459 | fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename); |
| 460 | } else { |
| 461 | fprintf(stderr,"[ERR] --read-firmware failed.\n"); |
| 462 | } |
Dave Chapman | 6e79715 | 2007-09-08 23:27:49 +0000 | [diff] [blame] | 463 | } else if (action==READ_AUPD) { |
| 464 | if (read_aupd(&ipod, filename)==0) { |
| 465 | fprintf(stderr,"[INFO] AUPD image read to file %s.\n",filename); |
| 466 | } else { |
| 467 | fprintf(stderr,"[ERR] --read-aupd failed.\n"); |
| 468 | } |
| 469 | } else if (action==WRITE_AUPD) { |
| 470 | if (ipod_reopen_rw(&ipod) < 0) { |
| 471 | return 5; |
| 472 | } |
| 473 | |
| 474 | if (write_aupd(&ipod, filename)==0) { |
| 475 | fprintf(stderr,"[INFO] AUPD image %s written to device.\n",filename); |
| 476 | } else { |
| 477 | fprintf(stderr,"[ERR] --write-aupd failed.\n"); |
| 478 | } |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 479 | } else if (action==READ_PARTITION) { |
| 480 | outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); |
| 481 | if (outfile < 0) { |
| 482 | perror(filename); |
| 483 | return 4; |
| 484 | } |
| 485 | |
| 486 | if (read_partition(&ipod, outfile) < 0) { |
| 487 | fprintf(stderr,"[ERR] --read-partition failed.\n"); |
| 488 | } else { |
| 489 | fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename); |
| 490 | } |
| 491 | close(outfile); |
| 492 | } else if (action==WRITE_PARTITION) { |
| 493 | if (ipod_reopen_rw(&ipod) < 0) { |
| 494 | return 5; |
| 495 | } |
| 496 | |
| 497 | infile = open(filename,O_RDONLY|O_BINARY); |
| 498 | if (infile < 0) { |
| 499 | perror(filename); |
| 500 | return 2; |
| 501 | } |
| 502 | |
| 503 | /* Check filesize is <= partition size */ |
| 504 | inputsize=filesize(infile); |
| 505 | if (inputsize > 0) { |
| 506 | if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) { |
| 507 | fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize); |
| 508 | if (write_partition(&ipod,infile) < 0) { |
| 509 | fprintf(stderr,"[ERR] --write-partition failed.\n"); |
| 510 | } else { |
| 511 | fprintf(stderr,"[INFO] %s restored to partition\n",filename); |
| 512 | } |
| 513 | } else { |
| 514 | fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n"); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | close(infile); |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 519 | } else if (action==FORMAT_PARTITION) { |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 520 | printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n"); |
| 521 | printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n"); |
| 522 | printf("Are you sure you want to format your ipod? (y/n):"); |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 523 | |
| 524 | if (fgets(yesno,4,stdin)) { |
| 525 | if (yesno[0]=='y') { |
| 526 | if (ipod_reopen_rw(&ipod) < 0) { |
| 527 | return 5; |
| 528 | } |
| 529 | |
| 530 | if (format_partition(&ipod,1) < 0) { |
| 531 | fprintf(stderr,"[ERR] Format failed.\n"); |
| 532 | } |
| 533 | } else { |
| 534 | fprintf(stderr,"[INFO] Format cancelled.\n"); |
| 535 | } |
| 536 | } |
Dave Chapman | 35735c66 | 2007-07-27 20:51:36 +0000 | [diff] [blame] | 537 | } else if (action==CONVERT_TO_FAT32) { |
| 538 | if (!ipod.macpod) { |
| 539 | printf("[ERR] Ipod is already FAT32, aborting\n"); |
| 540 | } else { |
| 541 | printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n"); |
| 542 | printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n"); |
| 543 | printf("Are you sure you want to convert your ipod to FAT32? (y/n):"); |
| 544 | |
| 545 | if (fgets(yesno,4,stdin)) { |
| 546 | if (yesno[0]=='y') { |
| 547 | if (ipod_reopen_rw(&ipod) < 0) { |
| 548 | return 5; |
| 549 | } |
| 550 | |
| 551 | if (write_dos_partition_table(&ipod) < 0) { |
| 552 | fprintf(stderr,"[ERR] Partition conversion failed.\n"); |
| 553 | } |
| 554 | |
| 555 | if (format_partition(&ipod,1) < 0) { |
| 556 | fprintf(stderr,"[ERR] Format failed.\n"); |
| 557 | } |
| 558 | } else { |
| 559 | fprintf(stderr,"[INFO] Format cancelled.\n"); |
| 560 | } |
| 561 | } |
| 562 | } |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | ipod_close(&ipod); |
| 566 | |
Dave Chapman | 6bd75aa | 2007-02-09 10:47:37 +0000 | [diff] [blame] | 567 | #ifdef WITH_BOOTOBJS |
| 568 | if (action==INTERACTIVE) { |
| 569 | printf("Press ENTER to exit ipodpatcher :"); |
| 570 | fgets(yesno,4,stdin); |
| 571 | } |
| 572 | #endif |
| 573 | |
| 574 | |
Dave Chapman | 79a1fb1 | 2007-02-08 23:57:41 +0000 | [diff] [blame] | 575 | return 0; |
| 576 | } |