diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-15 21:04:28 +0100 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-24 18:05:53 +0100 |
commit | c876d3bbefe0dc00c27ca0c12d29da5874946962 (patch) | |
tree | 69f468a185a369b01998314bc3ecc19b70f4fcaa /utils/ipodpatcher/main.c | |
parent | 6c6f0757d7a902feb293be165d1490c42bc8e7ad (diff) | |
download | rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.tar.gz rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.zip |
rbutil: Merge rbutil with utils folder.
rbutil uses several components from the utils folder, and can be
considered part of utils too. Having it in a separate folder is an
arbitrary split that doesn't help anymore these days, so merge them.
This also allows other utils to easily use libtools.make without the
need to navigate to a different folder.
Change-Id: I3fc2f4de19e3e776553efb5dea5f779dfec0dc21
Diffstat (limited to 'utils/ipodpatcher/main.c')
-rw-r--r-- | utils/ipodpatcher/main.c | 622 |
1 files changed, 622 insertions, 0 deletions
diff --git a/utils/ipodpatcher/main.c b/utils/ipodpatcher/main.c new file mode 100644 index 0000000000..7b0a909178 --- /dev/null +++ b/utils/ipodpatcher/main.c | |||
@@ -0,0 +1,622 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006-2007 Dave Chapman | ||
11 | * | ||
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. | ||
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 | |||
34 | #ifdef RELEASE | ||
35 | #undef VERSION | ||
36 | #define VERSION "5.0 with v4.0 bootloaders (v1.0 for 2nd Gen Nano)" | ||
37 | #endif | ||
38 | |||
39 | |||
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, | ||
52 | READ_AUPD, | ||
53 | WRITE_AUPD, | ||
54 | READ_PARTITION, | ||
55 | WRITE_PARTITION, | ||
56 | FORMAT_PARTITION, | ||
57 | DUMP_XML, | ||
58 | CONVERT_TO_FAT32 | ||
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/wiki/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"); | ||
87 | fprintf(stderr," -rf, --read-firmware filename.ipod[x]\n"); | ||
88 | fprintf(stderr," -rfb, --read-firmware-bin filename.bin\n"); | ||
89 | fprintf(stderr," -wf, --write-firmware filename.ipod[x]\n"); | ||
90 | fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n"); | ||
91 | #ifdef WITH_BOOTOBJS | ||
92 | fprintf(stderr," -we, --write-embedded\n"); | ||
93 | #endif | ||
94 | fprintf(stderr," -a, --add-bootloader filename.ipod[x]\n"); | ||
95 | fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n"); | ||
96 | fprintf(stderr," -d, --delete-bootloader\n"); | ||
97 | fprintf(stderr," -f, --format\n"); | ||
98 | fprintf(stderr," -c, --convert\n"); | ||
99 | fprintf(stderr," --read-aupd filename.bin\n"); | ||
100 | fprintf(stderr," --write-aupd filename.bin\n"); | ||
101 | fprintf(stderr," -x --dump-xml filename.xml\n"); | ||
102 | fprintf(stderr,"\n"); | ||
103 | |||
104 | fprintf(stderr,"The .ipodx extension is used for encrypted images for the 2nd Gen Nano.\n\n"); | ||
105 | |||
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 | |||
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, | ||
135 | (long int)ipod->pinfo[i].start, | ||
136 | (long int)ipod->pinfo[i].start+ipod->pinfo[i].size-1, | ||
137 | ipod->pinfo[i].size/sectors_per_MB, | ||
138 | get_parttype(ipod->pinfo[i].type), | ||
139 | (int)ipod->pinfo[i].type); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | |||
144 | |||
145 | int main(int argc, char* argv[]) | ||
146 | { | ||
147 | char yesno[4]; | ||
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 | |||
157 | fprintf(stderr,"ipodpatcher " VERSION "\n"); | ||
158 | fprintf(stderr,"(C) Dave Chapman 2006-2009\n"); | ||
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 IPOD_OK; | ||
165 | } | ||
166 | |||
167 | if (ipod_alloc_buffer(&ipod,BUFFER_SIZE) < 0) { | ||
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 IPOD_NOT_FOUND; | ||
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"); | ||
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"); | ||
200 | } else if (n > 1) { | ||
201 | fprintf(stderr,"[ERR] %d ipods found, aborting\n",n); | ||
202 | fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\n"); | ||
203 | return IPOD_MULTIPLE_DEVICES; | ||
204 | } else if (n == 1 && ipod.macpod) { | ||
205 | return IPOD_WRONG_TYPE; | ||
206 | } | ||
207 | |||
208 | if (n != 1) { | ||
209 | #ifdef WITH_BOOTOBJS | ||
210 | if (argc==1) { | ||
211 | printf("\nPress ENTER to exit ipodpatcher :"); | ||
212 | fgets(yesno,4,stdin); | ||
213 | } | ||
214 | #endif | ||
215 | return IPOD_NOT_FOUND; | ||
216 | } | ||
217 | |||
218 | i = 1; | ||
219 | } | ||
220 | |||
221 | #ifdef WITH_BOOTOBJS | ||
222 | action = INTERACTIVE; | ||
223 | #else | ||
224 | action = NONE; | ||
225 | #endif | ||
226 | |||
227 | while (i < argc) { | ||
228 | if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) { | ||
229 | action = LIST_IMAGES; | ||
230 | i++; | ||
231 | #ifdef WITH_BOOTOBJS | ||
232 | } else if (strcmp(argv[i],"--install")==0) { | ||
233 | action = INSTALL; | ||
234 | i++; | ||
235 | #endif | ||
236 | } else if ((strcmp(argv[i],"-d")==0) || | ||
237 | (strcmp(argv[i],"--delete-bootloader")==0)) { | ||
238 | action = DELETE_BOOTLOADER; | ||
239 | i++; | ||
240 | } else if ((strcmp(argv[i],"-a")==0) || | ||
241 | (strcmp(argv[i],"--add-bootloader")==0)) { | ||
242 | action = ADD_BOOTLOADER; | ||
243 | type = FILETYPE_DOT_IPOD; | ||
244 | i++; | ||
245 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
246 | filename=argv[i]; | ||
247 | i++; | ||
248 | } else if ((strcmp(argv[i],"-ab")==0) || | ||
249 | (strcmp(argv[i],"--add-bootloader-bin")==0)) { | ||
250 | action = ADD_BOOTLOADER; | ||
251 | type = FILETYPE_DOT_BIN; | ||
252 | i++; | ||
253 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
254 | filename=argv[i]; | ||
255 | i++; | ||
256 | } else if ((strcmp(argv[i],"-rf")==0) || | ||
257 | (strcmp(argv[i],"--read-firmware")==0)) { | ||
258 | action = READ_FIRMWARE; | ||
259 | type = FILETYPE_DOT_IPOD; | ||
260 | i++; | ||
261 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
262 | filename=argv[i]; | ||
263 | i++; | ||
264 | } else if ((strcmp(argv[i],"-rfb")==0) || | ||
265 | (strcmp(argv[i],"--read-firmware-bin")==0)) { | ||
266 | action = READ_FIRMWARE; | ||
267 | type = FILETYPE_DOT_BIN; | ||
268 | i++; | ||
269 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
270 | filename=argv[i]; | ||
271 | i++; | ||
272 | #ifdef WITH_BOOTOBJS | ||
273 | } else if ((strcmp(argv[i],"-we")==0) || | ||
274 | (strcmp(argv[i],"--write-embedded")==0)) { | ||
275 | action = WRITE_FIRMWARE; | ||
276 | type = FILETYPE_INTERNAL; | ||
277 | filename="[embedded bootloader]"; /* Only displayed for user */ | ||
278 | i++; | ||
279 | #endif | ||
280 | } else if ((strcmp(argv[i],"-wf")==0) || | ||
281 | (strcmp(argv[i],"--write-firmware")==0)) { | ||
282 | action = WRITE_FIRMWARE; | ||
283 | type = FILETYPE_DOT_IPOD; | ||
284 | i++; | ||
285 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
286 | filename=argv[i]; | ||
287 | i++; | ||
288 | } else if ((strcmp(argv[i],"-wfb")==0) || | ||
289 | (strcmp(argv[i],"--write-firmware-bin")==0)) { | ||
290 | action = WRITE_FIRMWARE; | ||
291 | type = FILETYPE_DOT_BIN; | ||
292 | i++; | ||
293 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
294 | filename=argv[i]; | ||
295 | i++; | ||
296 | } else if ((strcmp(argv[i],"-r")==0) || | ||
297 | (strcmp(argv[i],"--read-partition")==0)) { | ||
298 | action = READ_PARTITION; | ||
299 | i++; | ||
300 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
301 | filename=argv[i]; | ||
302 | i++; | ||
303 | } else if ((strcmp(argv[i],"-w")==0) || | ||
304 | (strcmp(argv[i],"--write-partition")==0)) { | ||
305 | action = WRITE_PARTITION; | ||
306 | i++; | ||
307 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
308 | filename=argv[i]; | ||
309 | i++; | ||
310 | } else if ((strcmp(argv[i],"-v")==0) || | ||
311 | (strcmp(argv[i],"--verbose")==0)) { | ||
312 | ipod_verbose++; | ||
313 | i++; | ||
314 | } else if ((strcmp(argv[i],"-f")==0) || | ||
315 | (strcmp(argv[i],"--format")==0)) { | ||
316 | action = FORMAT_PARTITION; | ||
317 | i++; | ||
318 | } else if (strcmp(argv[i],"--read-aupd")==0) { | ||
319 | action = READ_AUPD; | ||
320 | i++; | ||
321 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
322 | filename=argv[i]; | ||
323 | i++; | ||
324 | } else if (strcmp(argv[i],"--write-aupd")==0) { | ||
325 | action = WRITE_AUPD; | ||
326 | i++; | ||
327 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
328 | filename=argv[i]; | ||
329 | i++; | ||
330 | } else if ((strcmp(argv[i],"-x")==0) || | ||
331 | (strcmp(argv[i],"--dump-xml")==0)) { | ||
332 | action = DUMP_XML; | ||
333 | i++; | ||
334 | if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; } | ||
335 | filename=argv[i]; | ||
336 | i++; | ||
337 | } else if ((strcmp(argv[i],"-c")==0) || | ||
338 | (strcmp(argv[i],"--convert")==0)) { | ||
339 | action = CONVERT_TO_FAT32; | ||
340 | i++; | ||
341 | } else { | ||
342 | print_usage(); return IPOD_WRONG_ARGUMENTS; | ||
343 | } | ||
344 | } | ||
345 | |||
346 | if (ipod.diskname[0]==0) { | ||
347 | print_usage(); | ||
348 | return 1; | ||
349 | } | ||
350 | |||
351 | if (ipod_open(&ipod, 0) < 0) { | ||
352 | return IPOD_ACCESS_DENIED; | ||
353 | } | ||
354 | |||
355 | fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname); | ||
356 | fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size); | ||
357 | |||
358 | if (read_partinfo(&ipod,0) < 0) { | ||
359 | return IPOD_PARTITION_ERROR; | ||
360 | } | ||
361 | |||
362 | display_partinfo(&ipod); | ||
363 | |||
364 | if (ipod.pinfo[0].start==0) { | ||
365 | fprintf(stderr,"[ERR] No partition 0 on disk:\n"); | ||
366 | display_partinfo(&ipod); | ||
367 | return IPOD_PARTITION_ERROR; | ||
368 | } | ||
369 | |||
370 | read_directory(&ipod); | ||
371 | |||
372 | if (ipod.nimages <= 0) { | ||
373 | fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages); | ||
374 | return IPOD_IMAGE_ERROR; | ||
375 | } | ||
376 | |||
377 | if (getmodel(&ipod,(ipod.ipod_directory[ipod.ososimage].vers>>8)) < 0) { | ||
378 | fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n", | ||
379 | ipod.ipod_directory[ipod.ososimage].vers); | ||
380 | return IPOD_UNKNOWN_FW_VERSION; | ||
381 | } | ||
382 | |||
383 | #ifdef __WIN32__ | ||
384 | /* Windows requires the ipod in R/W mode for SCSI Inquiry */ | ||
385 | if (ipod_reopen_rw(&ipod) < 0) { | ||
386 | return IPOD_CANNOT_REOPEN; | ||
387 | } | ||
388 | #endif | ||
389 | |||
390 | |||
391 | /* Read the XML info, and if successful, look for the ramsize | ||
392 | (only available for some models - set to 0 if not known) */ | ||
393 | |||
394 | ipod.ramsize = 0; | ||
395 | |||
396 | if (ipod_get_xmlinfo(&ipod) == 0) { | ||
397 | ipod_get_ramsize(&ipod); | ||
398 | } | ||
399 | |||
400 | printf("[INFO] Ipod model: %s ",ipod.modelstr); | ||
401 | if (ipod.ramsize > 0) { printf("(%dMB RAM) ",ipod.ramsize); } | ||
402 | printf("(\"%s\")\n",ipod.macpod ? "macpod" : "winpod"); | ||
403 | |||
404 | if (ipod.macpod) { | ||
405 | print_macpod_warning(); | ||
406 | } | ||
407 | |||
408 | if (action==LIST_IMAGES) { | ||
409 | list_images(&ipod); | ||
410 | #ifdef WITH_BOOTOBJS | ||
411 | } else if (action==INTERACTIVE) { | ||
412 | |||
413 | printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :"); | ||
414 | |||
415 | if (fgets(yesno,4,stdin)) { | ||
416 | if (yesno[0]=='i') { | ||
417 | if (ipod_reopen_rw(&ipod) < 0) { | ||
418 | return IPOD_CANNOT_REOPEN; | ||
419 | } | ||
420 | |||
421 | if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { | ||
422 | fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); | ||
423 | } else { | ||
424 | fprintf(stderr,"[ERR] --install failed.\n"); | ||
425 | } | ||
426 | } else if (yesno[0]=='u') { | ||
427 | if (ipod_reopen_rw(&ipod) < 0) { | ||
428 | return IPOD_CANNOT_REOPEN; | ||
429 | } | ||
430 | |||
431 | if (delete_bootloader(&ipod)==0) { | ||
432 | fprintf(stderr,"[INFO] Bootloader removed.\n"); | ||
433 | } else { | ||
434 | fprintf(stderr,"[ERR] Bootloader removal failed.\n"); | ||
435 | } | ||
436 | } | ||
437 | } | ||
438 | #endif | ||
439 | } else if (action==DELETE_BOOTLOADER) { | ||
440 | if (ipod_reopen_rw(&ipod) < 0) { | ||
441 | return IPOD_CANNOT_REOPEN; | ||
442 | } | ||
443 | |||
444 | if (ipod.ipod_directory[0].entryOffset==0) { | ||
445 | fprintf(stderr,"[ERR] No bootloader detected.\n"); | ||
446 | } else { | ||
447 | if (delete_bootloader(&ipod)==0) { | ||
448 | fprintf(stderr,"[INFO] Bootloader removed.\n"); | ||
449 | } else { | ||
450 | fprintf(stderr,"[ERR] --delete-bootloader failed.\n"); | ||
451 | } | ||
452 | } | ||
453 | } else if (action==ADD_BOOTLOADER) { | ||
454 | if (ipod_reopen_rw(&ipod) < 0) { | ||
455 | return IPOD_CANNOT_REOPEN; | ||
456 | } | ||
457 | |||
458 | if (add_bootloader(&ipod, filename, type)==0) { | ||
459 | fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename); | ||
460 | } else { | ||
461 | fprintf(stderr,"[ERR] --add-bootloader failed.\n"); | ||
462 | } | ||
463 | #ifdef WITH_BOOTOBJS | ||
464 | } else if (action==INSTALL) { | ||
465 | if (ipod_reopen_rw(&ipod) < 0) { | ||
466 | return IPOD_CANNOT_REOPEN; | ||
467 | } | ||
468 | |||
469 | if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { | ||
470 | fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); | ||
471 | } else { | ||
472 | fprintf(stderr,"[ERR] --install failed.\n"); | ||
473 | } | ||
474 | #endif | ||
475 | } else if (action==WRITE_FIRMWARE) { | ||
476 | if (ipod_reopen_rw(&ipod) < 0) { | ||
477 | return IPOD_CANNOT_REOPEN; | ||
478 | } | ||
479 | |||
480 | if (write_firmware(&ipod, filename,type)==0) { | ||
481 | fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename); | ||
482 | } else { | ||
483 | fprintf(stderr,"[ERR] --write-firmware failed.\n"); | ||
484 | } | ||
485 | } else if (action==READ_FIRMWARE) { | ||
486 | if (read_firmware(&ipod, filename, type)==0) { | ||
487 | fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename); | ||
488 | } else { | ||
489 | fprintf(stderr,"[ERR] --read-firmware failed.\n"); | ||
490 | } | ||
491 | } else if (action==READ_AUPD) { | ||
492 | if (read_aupd(&ipod, filename)==0) { | ||
493 | fprintf(stderr,"[INFO] AUPD image read to file %s.\n",filename); | ||
494 | } else { | ||
495 | fprintf(stderr,"[ERR] --read-aupd failed.\n"); | ||
496 | } | ||
497 | } else if (action==WRITE_AUPD) { | ||
498 | if (ipod_reopen_rw(&ipod) < 0) { | ||
499 | return IPOD_CANNOT_REOPEN; | ||
500 | } | ||
501 | |||
502 | if (write_aupd(&ipod, filename)==0) { | ||
503 | fprintf(stderr,"[INFO] AUPD image %s written to device.\n",filename); | ||
504 | } else { | ||
505 | fprintf(stderr,"[ERR] --write-aupd failed.\n"); | ||
506 | } | ||
507 | } else if (action==DUMP_XML) { | ||
508 | if (ipod.xmlinfo == NULL) { | ||
509 | fprintf(stderr,"[ERR] No XML to write\n"); | ||
510 | return IPOD_DUMP_FAILED; | ||
511 | } | ||
512 | |||
513 | outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); | ||
514 | if (outfile < 0) { | ||
515 | perror(filename); | ||
516 | return IPOD_OPEN_OUTFILE_FAILED; | ||
517 | } | ||
518 | |||
519 | if (write(outfile, ipod.xmlinfo, ipod.xmlinfo_len) < 0) { | ||
520 | fprintf(stderr,"[ERR] --dump-xml failed.\n"); | ||
521 | } else { | ||
522 | fprintf(stderr,"[INFO] XML info written to %s.\n",filename); | ||
523 | } | ||
524 | close(outfile); | ||
525 | } else if (action==READ_PARTITION) { | ||
526 | outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); | ||
527 | if (outfile < 0) { | ||
528 | perror(filename); | ||
529 | return IPOD_OPEN_OUTFILE_FAILED; | ||
530 | } | ||
531 | |||
532 | if (read_partition(&ipod, outfile) < 0) { | ||
533 | fprintf(stderr,"[ERR] --read-partition failed.\n"); | ||
534 | } else { | ||
535 | fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename); | ||
536 | } | ||
537 | close(outfile); | ||
538 | } else if (action==WRITE_PARTITION) { | ||
539 | if (ipod_reopen_rw(&ipod) < 0) { | ||
540 | return IPOD_CANNOT_REOPEN; | ||
541 | } | ||
542 | |||
543 | infile = open(filename,O_RDONLY|O_BINARY); | ||
544 | if (infile < 0) { | ||
545 | perror(filename); | ||
546 | return IPOD_OPEN_INFILE_FAILED; | ||
547 | } | ||
548 | |||
549 | /* Check filesize is <= partition size */ | ||
550 | inputsize=filesize(infile); | ||
551 | if (inputsize > 0) { | ||
552 | if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) { | ||
553 | fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize); | ||
554 | if (write_partition(&ipod,infile) < 0) { | ||
555 | fprintf(stderr,"[ERR] --write-partition failed.\n"); | ||
556 | } else { | ||
557 | fprintf(stderr,"[INFO] %s restored to partition\n",filename); | ||
558 | } | ||
559 | } else { | ||
560 | fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n"); | ||
561 | } | ||
562 | } | ||
563 | |||
564 | close(infile); | ||
565 | } else if (action==FORMAT_PARTITION) { | ||
566 | printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n"); | ||
567 | printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n"); | ||
568 | printf("Are you sure you want to format your ipod? (y/n):"); | ||
569 | |||
570 | if (fgets(yesno,4,stdin)) { | ||
571 | if (yesno[0]=='y') { | ||
572 | if (ipod_reopen_rw(&ipod) < 0) { | ||
573 | return IPOD_CANNOT_REOPEN; | ||
574 | } | ||
575 | |||
576 | if (format_partition(&ipod,1) < 0) { | ||
577 | fprintf(stderr,"[ERR] Format failed.\n"); | ||
578 | } | ||
579 | } else { | ||
580 | fprintf(stderr,"[INFO] Format cancelled.\n"); | ||
581 | } | ||
582 | } | ||
583 | } else if (action==CONVERT_TO_FAT32) { | ||
584 | if (!ipod.macpod) { | ||
585 | printf("[ERR] Ipod is already FAT32, aborting\n"); | ||
586 | } else { | ||
587 | printf("WARNING!!! YOU ARE ABOUT TO USE AN EXPERIMENTAL FEATURE.\n"); | ||
588 | printf("ALL DATA ON YOUR IPOD WILL BE ERASED.\n"); | ||
589 | printf("Are you sure you want to convert your ipod to FAT32? (y/n):"); | ||
590 | |||
591 | if (fgets(yesno,4,stdin)) { | ||
592 | if (yesno[0]=='y') { | ||
593 | if (ipod_reopen_rw(&ipod) < 0) { | ||
594 | return IPOD_CANNOT_REOPEN; | ||
595 | } | ||
596 | |||
597 | if (write_dos_partition_table(&ipod) < 0) { | ||
598 | fprintf(stderr,"[ERR] Partition conversion failed.\n"); | ||
599 | } | ||
600 | |||
601 | if (format_partition(&ipod,1) < 0) { | ||
602 | fprintf(stderr,"[ERR] Format failed.\n"); | ||
603 | } | ||
604 | } else { | ||
605 | fprintf(stderr,"[INFO] Format cancelled.\n"); | ||
606 | } | ||
607 | } | ||
608 | } | ||
609 | } | ||
610 | |||
611 | ipod_close(&ipod); | ||
612 | |||
613 | #ifdef WITH_BOOTOBJS | ||
614 | if (action==INTERACTIVE) { | ||
615 | printf("Press ENTER to exit ipodpatcher :"); | ||
616 | fgets(yesno,4,stdin); | ||
617 | } | ||
618 | #endif | ||
619 | |||
620 | ipod_dealloc_buffer(&ipod); | ||
621 | return IPOD_OK; | ||
622 | } | ||