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