same as previous commit, but more forgotten cases
[Rockbox.git] / rbutil / ipodpatcher / main.c
bloba4508abf57279b35dd10a8713fd2bdeb3562e8ec
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 "1.1-svn"
34 int verbose = 0;
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_PARTITION,
49 WRITE_PARTITION,
50 FORMAT_PARTITION
53 void print_macpod_warning(void)
55 printf("[INFO] ************************************************************************\n");
56 printf("[INFO] *** WARNING FOR ROCKBOX USERS\n");
57 printf("[INFO] *** You must convert this ipod to FAT32 format (aka a \"winpod\")\n");
58 printf("[INFO] *** if you want to run Rockbox. Rockbox WILL NOT work on this ipod.\n");
59 printf("[INFO] *** See http://www.rockbox.org/twiki/bin/view/Main/IpodConversionToFAT32\n");
60 printf("[INFO] ************************************************************************\n");
63 void print_usage(void)
65 fprintf(stderr,"Usage: ipodpatcher --scan\n");
66 #ifdef __WIN32__
67 fprintf(stderr," or ipodpatcher [DISKNO] [action]\n");
68 #else
69 fprintf(stderr," or ipodpatcher [device] [action]\n");
70 #endif
71 fprintf(stderr,"\n");
72 fprintf(stderr,"Where [action] is one of the following options:\n");
73 #ifdef WITH_BOOTOBJS
74 fprintf(stderr," --install\n");
75 #endif
76 fprintf(stderr," -l, --list\n");
77 fprintf(stderr," -r, --read-partition bootpartition.bin\n");
78 fprintf(stderr," -w, --write-partition bootpartition.bin\n");
79 fprintf(stderr," -rf, --read-firmware filename.ipod\n");
80 fprintf(stderr," -rfb, --read-firmware-bin filename.bin\n");
81 fprintf(stderr," -wf, --write-firmware filename.ipod\n");
82 fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n");
83 #ifdef WITH_BOOTOBJS
84 fprintf(stderr," -we, --write-embedded\n");
85 #endif
86 fprintf(stderr," -a, --add-bootloader filename.ipod\n");
87 fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n");
88 fprintf(stderr," -d, --delete-bootloader\n");
89 fprintf(stderr," -f, --format\n");
90 fprintf(stderr,"\n");
92 #ifdef __WIN32__
93 fprintf(stderr,"DISKNO is the number (e.g. 2) Windows has assigned to your ipod's hard disk.\n");
94 fprintf(stderr,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
95 fprintf(stderr,"will be disk 1 etc. ipodpatcher will refuse to access a disk unless it\n");
96 fprintf(stderr,"can identify it as being an ipod.\n");
97 fprintf(stderr,"\n");
98 #else
99 #if defined(linux) || defined (__linux)
100 fprintf(stderr,"\"device\" is the device node (e.g. /dev/sda) assigned to your ipod.\n");
101 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
102 fprintf(stderr,"\"device\" is the device node (e.g. /dev/da1) assigned to your ipod.\n");
103 #elif defined(__APPLE__) && defined(__MACH__)
104 fprintf(stderr,"\"device\" is the device node (e.g. /dev/disk1) assigned to your ipod.\n");
105 #endif
106 fprintf(stderr,"ipodpatcher will refuse to access a disk unless it can identify it as being\n");
107 fprintf(stderr,"an ipod.\n");
108 #endif
111 void display_partinfo(struct ipod_t* ipod)
113 int i;
114 double sectors_per_MB = (1024.0*1024.0)/ipod->sector_size;
116 printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
117 for ( i = 0; i < 4; i++ ) {
118 if (ipod->pinfo[i].start != 0) {
119 printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
121 ipod->pinfo[i].start,
122 ipod->pinfo[i].start+ipod->pinfo[i].size-1,
123 ipod->pinfo[i].size/sectors_per_MB,
124 get_parttype(ipod->pinfo[i].type),
125 ipod->pinfo[i].type);
131 int main(int argc, char* argv[])
133 char yesno[4];
134 int i;
135 int n;
136 int infile, outfile;
137 unsigned int inputsize;
138 char* filename;
139 int action = SHOW_INFO;
140 int type;
141 struct ipod_t ipod;
143 fprintf(stderr,"ipodpatcher v" VERSION " - (C) Dave Chapman 2006-2007\n");
144 fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n");
145 fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
147 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
148 print_usage();
149 return 1;
152 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) {
153 fprintf(stderr,"Failed to allocate memory buffer\n");
156 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
157 if (ipod_scan(&ipod) == 0)
158 fprintf(stderr,"[ERR] No ipods found.\n");
159 return 0;
162 /* If the first parameter doesn't start with -, then we interpret it as a device */
163 if ((argc > 1) && (argv[1][0] != '-')) {
164 ipod.diskname[0]=0;
165 #ifdef __WIN32__
166 snprintf(ipod.diskname,sizeof(ipod.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
167 #else
168 strncpy(ipod.diskname,argv[1],sizeof(ipod.diskname));
169 #endif
170 i = 2;
171 } else {
172 /* Autoscan for ipods */
173 n = ipod_scan(&ipod);
174 if (n==0) {
175 fprintf(stderr,"[ERR] No ipods found, aborting\n");
176 fprintf(stderr,"[ERR] Please connect your ipod and ensure it is in disk mode\n");
177 #if defined(__APPLE__) && defined(__MACH__)
178 fprintf(stderr,"[ERR] Also ensure that itunes is closed, and that your ipod is not mounted.\n");
179 #elif !defined(__WIN32__)
180 if (geteuid()!=0) {
181 fprintf(stderr,"[ERR] You may also need to run ipodpatcher as root.\n");
183 #endif
184 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
185 } else if (n > 1) {
186 fprintf(stderr,"[ERR] %d ipods found, aborting\n",n);
187 fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\n");
190 if (n != 1) {
191 #ifdef WITH_BOOTOBJS
192 if (argc==1) {
193 printf("\nPress ENTER to exit ipodpatcher :");
194 fgets(yesno,4,stdin);
196 #endif
197 return 0;
200 i = 1;
203 #ifdef WITH_BOOTOBJS
204 action = INTERACTIVE;
205 #else
206 action = NONE;
207 #endif
209 while (i < argc) {
210 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
211 action = LIST_IMAGES;
212 i++;
213 #ifdef WITH_BOOTOBJS
214 } else if (strcmp(argv[i],"--install")==0) {
215 action = INSTALL;
216 i++;
217 #endif
218 } else if ((strcmp(argv[i],"-d")==0) ||
219 (strcmp(argv[i],"--delete-bootloader")==0)) {
220 action = DELETE_BOOTLOADER;
221 i++;
222 } else if ((strcmp(argv[i],"-a")==0) ||
223 (strcmp(argv[i],"--add-bootloader")==0)) {
224 action = ADD_BOOTLOADER;
225 type = FILETYPE_DOT_IPOD;
226 i++;
227 if (i == argc) { print_usage(); return 1; }
228 filename=argv[i];
229 i++;
230 } else if ((strcmp(argv[i],"-ab")==0) ||
231 (strcmp(argv[i],"--add-bootloader-bin")==0)) {
232 action = ADD_BOOTLOADER;
233 type = FILETYPE_DOT_BIN;
234 i++;
235 if (i == argc) { print_usage(); return 1; }
236 filename=argv[i];
237 i++;
238 } else if ((strcmp(argv[i],"-rf")==0) ||
239 (strcmp(argv[i],"--read-firmware")==0)) {
240 action = READ_FIRMWARE;
241 type = FILETYPE_DOT_IPOD;
242 i++;
243 if (i == argc) { print_usage(); return 1; }
244 filename=argv[i];
245 i++;
246 } else if ((strcmp(argv[i],"-rfb")==0) ||
247 (strcmp(argv[i],"--read-firmware-bin")==0)) {
248 action = READ_FIRMWARE;
249 type = FILETYPE_DOT_BIN;
250 i++;
251 if (i == argc) { print_usage(); return 1; }
252 filename=argv[i];
253 i++;
254 #ifdef WITH_BOOTOBJS
255 } else if ((strcmp(argv[i],"-we")==0) ||
256 (strcmp(argv[i],"--write-embedded")==0)) {
257 action = WRITE_FIRMWARE;
258 type = FILETYPE_INTERNAL;
259 filename="[embedded bootloader]"; /* Only displayed for user */
260 i++;
261 #endif
262 } else if ((strcmp(argv[i],"-wf")==0) ||
263 (strcmp(argv[i],"--write-firmware")==0)) {
264 action = WRITE_FIRMWARE;
265 type = FILETYPE_DOT_IPOD;
266 i++;
267 if (i == argc) { print_usage(); return 1; }
268 filename=argv[i];
269 i++;
270 } else if ((strcmp(argv[i],"-wfb")==0) ||
271 (strcmp(argv[i],"--write-firmware-bin")==0)) {
272 action = WRITE_FIRMWARE;
273 type = FILETYPE_DOT_BIN;
274 i++;
275 if (i == argc) { print_usage(); return 1; }
276 filename=argv[i];
277 i++;
278 } else if ((strcmp(argv[i],"-r")==0) ||
279 (strcmp(argv[i],"--read-partition")==0)) {
280 action = READ_PARTITION;
281 i++;
282 if (i == argc) { print_usage(); return 1; }
283 filename=argv[i];
284 i++;
285 } else if ((strcmp(argv[i],"-w")==0) ||
286 (strcmp(argv[i],"--write-partition")==0)) {
287 action = WRITE_PARTITION;
288 i++;
289 if (i == argc) { print_usage(); return 1; }
290 filename=argv[i];
291 i++;
292 } else if ((strcmp(argv[i],"-v")==0) ||
293 (strcmp(argv[i],"--verbose")==0)) {
294 verbose++;
295 i++;
296 } else if ((strcmp(argv[i],"-f")==0) ||
297 (strcmp(argv[i],"--format")==0)) {
298 action = FORMAT_PARTITION;
299 i++;
300 } else {
301 print_usage(); return 1;
305 if (ipod.diskname[0]==0) {
306 print_usage();
307 return 1;
310 if (ipod_open(&ipod, 0) < 0) {
311 return 1;
314 fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname);
315 fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size);
317 if (read_partinfo(&ipod,0) < 0) {
318 return 2;
321 display_partinfo(&ipod);
323 if (ipod.pinfo[0].start==0) {
324 fprintf(stderr,"[ERR] No partition 0 on disk:\n");
325 display_partinfo(&ipod);
326 return 3;
329 read_directory(&ipod);
331 if (ipod.nimages <= 0) {
332 fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages);
333 return 1;
336 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) {
337 fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n",
338 ipod.ipod_directory[0].vers);
339 return -1;
342 printf("[INFO] Ipod model: %s (\"%s\")\n",ipod.modelstr,
343 ipod.macpod ? "macpod" : "winpod");
345 if (ipod.macpod) {
346 print_macpod_warning();
349 if (action==LIST_IMAGES) {
350 list_images(&ipod);
351 #ifdef WITH_BOOTOBJS
352 } else if (action==INTERACTIVE) {
354 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
356 if (fgets(yesno,4,stdin)) {
357 if (yesno[0]=='i') {
358 if (ipod_reopen_rw(&ipod) < 0) {
359 return 5;
362 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
363 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
364 } else {
365 fprintf(stderr,"[ERR] --install failed.\n");
367 } else if (yesno[0]=='u') {
368 if (ipod_reopen_rw(&ipod) < 0) {
369 return 5;
372 if (delete_bootloader(&ipod)==0) {
373 fprintf(stderr,"[INFO] Bootloader removed.\n");
374 } else {
375 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
379 #endif
380 } else if (action==DELETE_BOOTLOADER) {
381 if (ipod_reopen_rw(&ipod) < 0) {
382 return 5;
385 if (ipod.ipod_directory[0].entryOffset==0) {
386 fprintf(stderr,"[ERR] No bootloader detected.\n");
387 } else {
388 if (delete_bootloader(&ipod)==0) {
389 fprintf(stderr,"[INFO] Bootloader removed.\n");
390 } else {
391 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
394 } else if (action==ADD_BOOTLOADER) {
395 if (ipod_reopen_rw(&ipod) < 0) {
396 return 5;
399 if (add_bootloader(&ipod, filename, type)==0) {
400 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
401 } else {
402 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
404 #ifdef WITH_BOOTOBJS
405 } else if (action==INSTALL) {
406 if (ipod_reopen_rw(&ipod) < 0) {
407 return 5;
410 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
411 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
412 } else {
413 fprintf(stderr,"[ERR] --install failed.\n");
415 #endif
416 } else if (action==WRITE_FIRMWARE) {
417 if (ipod_reopen_rw(&ipod) < 0) {
418 return 5;
421 if (write_firmware(&ipod, filename,type)==0) {
422 fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename);
423 } else {
424 fprintf(stderr,"[ERR] --write-firmware failed.\n");
426 } else if (action==READ_FIRMWARE) {
427 if (read_firmware(&ipod, filename, type)==0) {
428 fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename);
429 } else {
430 fprintf(stderr,"[ERR] --read-firmware failed.\n");
432 } else if (action==READ_PARTITION) {
433 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
434 if (outfile < 0) {
435 perror(filename);
436 return 4;
439 if (read_partition(&ipod, outfile) < 0) {
440 fprintf(stderr,"[ERR] --read-partition failed.\n");
441 } else {
442 fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename);
444 close(outfile);
445 } else if (action==WRITE_PARTITION) {
446 if (ipod_reopen_rw(&ipod) < 0) {
447 return 5;
450 infile = open(filename,O_RDONLY|O_BINARY);
451 if (infile < 0) {
452 perror(filename);
453 return 2;
456 /* Check filesize is <= partition size */
457 inputsize=filesize(infile);
458 if (inputsize > 0) {
459 if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) {
460 fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize);
461 if (write_partition(&ipod,infile) < 0) {
462 fprintf(stderr,"[ERR] --write-partition failed.\n");
463 } else {
464 fprintf(stderr,"[INFO] %s restored to partition\n",filename);
466 } else {
467 fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n");
471 close(infile);
472 } else if (action==FORMAT_PARTITION) {
473 printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FORMATTING FEATURE.\n");
474 printf("Are you sure you want to continue? (y/n):");
476 if (fgets(yesno,4,stdin)) {
477 if (yesno[0]=='y') {
478 if (ipod_reopen_rw(&ipod) < 0) {
479 return 5;
482 if (format_partition(&ipod,1) < 0) {
483 fprintf(stderr,"[ERR] Format failed.\n");
485 } else {
486 fprintf(stderr,"[INFO] Format cancelled.\n");
491 ipod_close(&ipod);
493 #ifdef WITH_BOOTOBJS
494 if (action==INTERACTIVE) {
495 printf("Press ENTER to exit ipodpatcher :");
496 fgets(yesno,4,stdin);
498 #endif
501 return 0;