diff options
author | Dave Chapman <dave@dchapman.com> | 2007-02-10 19:49:43 +0000 |
---|---|---|
committer | Dave Chapman <dave@dchapman.com> | 2007-02-10 19:49:43 +0000 |
commit | 955afed1b74793a3f06f98f3c259ec98f26a0b82 (patch) | |
tree | 6dcaa6b318ce282b21327530171f4f6a3297cb04 /rbutil/ipodpatcher/main.c | |
parent | f0efde23039064d393e8cc35c2d88a487c47b1c7 (diff) | |
download | rockbox-955afed1b74793a3f06f98f3c259ec98f26a0b82.tar.gz rockbox-955afed1b74793a3f06f98f3c259ec98f26a0b82.zip |
Move ipodpatcher into rbutil directory
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12263 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/ipodpatcher/main.c')
-rw-r--r-- | rbutil/ipodpatcher/main.c | 429 |
1 files changed, 429 insertions, 0 deletions
diff --git a/rbutil/ipodpatcher/main.c b/rbutil/ipodpatcher/main.c new file mode 100644 index 0000000000..d183b9bc52 --- /dev/null +++ b/rbutil/ipodpatcher/main.c | |||
@@ -0,0 +1,429 @@ | |||
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 | |||
32 | #define VERSION "0.8 with r12194-070204 bootloaders" | ||
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, | ||
48 | READ_PARTITION, | ||
49 | WRITE_PARTITION | ||
50 | }; | ||
51 | |||
52 | void print_macpod_warning(void) | ||
53 | { | ||
54 | printf("[INFO] ************************************************************************\n"); | ||
55 | printf("[INFO] *** WARNING FOR ROCKBOX USERS\n"); | ||
56 | printf("[INFO] *** You must convert this ipod to FAT32 format (aka a \"winpod\")\n"); | ||
57 | printf("[INFO] *** if you want to run Rockbox. Rockbox WILL NOT work on this ipod.\n"); | ||
58 | printf("[INFO] *** See http://www.rockbox.org/twiki/bin/view/Main/IpodConversionToFAT32\n"); | ||
59 | printf("[INFO] ************************************************************************\n"); | ||
60 | } | ||
61 | |||
62 | void print_usage(void) | ||
63 | { | ||
64 | fprintf(stderr,"Usage: ipodpatcher --scan\n"); | ||
65 | #ifdef __WIN32__ | ||
66 | fprintf(stderr," or ipodpatcher [DISKNO] [action]\n"); | ||
67 | #else | ||
68 | fprintf(stderr," or ipodpatcher [device] [action]\n"); | ||
69 | #endif | ||
70 | fprintf(stderr,"\n"); | ||
71 | fprintf(stderr,"Where [action] is one of the following options:\n"); | ||
72 | #ifdef WITH_BOOTOBJS | ||
73 | fprintf(stderr," --install\n"); | ||
74 | #endif | ||
75 | fprintf(stderr," -l, --list\n"); | ||
76 | fprintf(stderr," -r, --read-partition bootpartition.bin\n"); | ||
77 | fprintf(stderr," -w, --write-partition bootpartition.bin\n"); | ||
78 | fprintf(stderr," -rf, --read-firmware filename.ipod\n"); | ||
79 | fprintf(stderr," -wf, --write-firmware filename.ipod\n"); | ||
80 | fprintf(stderr," -wfb, --write-firmware-bin filename.bin\n"); | ||
81 | fprintf(stderr," -a, --add-bootloader filename.ipod\n"); | ||
82 | fprintf(stderr," -ab, --add-bootloader-bin filename.bin\n"); | ||
83 | fprintf(stderr," -d, --delete-bootloader\n"); | ||
84 | fprintf(stderr,"\n"); | ||
85 | |||
86 | #ifdef __WIN32__ | ||
87 | fprintf(stderr,"DISKNO is the number (e.g. 2) Windows has assigned to your ipod's hard disk.\n"); | ||
88 | fprintf(stderr,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n"); | ||
89 | fprintf(stderr,"will be disk 1 etc. ipodpatcher will refuse to access a disk unless it\n"); | ||
90 | fprintf(stderr,"can identify it as being an ipod.\n"); | ||
91 | fprintf(stderr,"\n"); | ||
92 | #else | ||
93 | #if defined(linux) || defined (__linux) | ||
94 | fprintf(stderr,"\"device\" is the device node (e.g. /dev/sda) assigned to your ipod.\n"); | ||
95 | #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) | ||
96 | fprintf(stderr,"\"device\" is the device node (e.g. /dev/da1) assigned to your ipod.\n"); | ||
97 | #elif defined(__APPLE__) && defined(__MACH__) | ||
98 | fprintf(stderr,"\"device\" is the device node (e.g. /dev/disk1) assigned to your ipod.\n"); | ||
99 | #endif | ||
100 | fprintf(stderr,"ipodpatcher will refuse to access a disk unless it can identify it as being\n"); | ||
101 | fprintf(stderr,"an ipod.\n"); | ||
102 | #endif | ||
103 | } | ||
104 | |||
105 | int main(int argc, char* argv[]) | ||
106 | { | ||
107 | #ifdef WITH_BOOTOBJS | ||
108 | char yesno[4]; | ||
109 | #endif | ||
110 | int i; | ||
111 | int n; | ||
112 | int infile, outfile; | ||
113 | unsigned int inputsize; | ||
114 | char* filename; | ||
115 | int action = SHOW_INFO; | ||
116 | int type; | ||
117 | struct ipod_t ipod; | ||
118 | |||
119 | fprintf(stderr,"ipodpatcher v" VERSION " - (C) Dave Chapman 2006-2007\n"); | ||
120 | fprintf(stderr,"This is free software; see the source for copying conditions. There is NO\n"); | ||
121 | fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); | ||
122 | |||
123 | if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) { | ||
124 | print_usage(); | ||
125 | return 1; | ||
126 | } | ||
127 | |||
128 | if (ipod_alloc_buffer(§orbuf,BUFFER_SIZE) < 0) { | ||
129 | fprintf(stderr,"Failed to allocate memory buffer\n"); | ||
130 | } | ||
131 | |||
132 | if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) { | ||
133 | if (ipod_scan(&ipod) == 0) | ||
134 | fprintf(stderr,"[ERR] No ipods found.\n"); | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | /* If the first parameter doesn't start with -, then we interpret it as a device */ | ||
139 | if ((argc > 1) && (argv[1][0] != '-')) { | ||
140 | ipod.diskname[0]=0; | ||
141 | #ifdef __WIN32__ | ||
142 | snprintf(ipod.diskname,sizeof(ipod.diskname),"\\\\.\\PhysicalDrive%s",argv[1]); | ||
143 | #else | ||
144 | strncpy(ipod.diskname,argv[1],sizeof(ipod.diskname)); | ||
145 | #endif | ||
146 | i = 2; | ||
147 | } else { | ||
148 | /* Autoscan for ipods */ | ||
149 | n = ipod_scan(&ipod); | ||
150 | if (n==0) { | ||
151 | fprintf(stderr,"[ERR] No ipods found, aborting\n"); | ||
152 | fprintf(stderr,"[ERR] Please connect your ipod and ensure it is in disk mode\n"); | ||
153 | #if defined(__APPLE__) && defined(__MACH__) | ||
154 | fprintf(stderr,"[ERR] Also ensure that itunes is closed, and that your ipod is not mounted.\n"); | ||
155 | #elif !defined(__WIN32__) | ||
156 | if (geteuid()!=0) { | ||
157 | fprintf(stderr,"[ERR] You may also need to run ipodpatcher as root.\n"); | ||
158 | } | ||
159 | #endif | ||
160 | fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n"); | ||
161 | } else if (n > 1) { | ||
162 | fprintf(stderr,"[ERR] %d ipods found, aborting\n",n); | ||
163 | fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\n"); | ||
164 | } | ||
165 | |||
166 | if (n != 1) { | ||
167 | #ifdef WITH_BOOTOBJS | ||
168 | if (argc==1) { | ||
169 | printf("\nPress ENTER to exit ipodpatcher :"); | ||
170 | fgets(yesno,4,stdin); | ||
171 | } | ||
172 | #endif | ||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | i = 1; | ||
177 | } | ||
178 | |||
179 | #ifdef WITH_BOOTOBJS | ||
180 | action = INTERACTIVE; | ||
181 | #else | ||
182 | action = NONE; | ||
183 | #endif | ||
184 | |||
185 | while (i < argc) { | ||
186 | if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) { | ||
187 | action = LIST_IMAGES; | ||
188 | i++; | ||
189 | #ifdef WITH_BOOTOBJS | ||
190 | } else if (strcmp(argv[i],"--install")==0) { | ||
191 | action = INSTALL; | ||
192 | i++; | ||
193 | #endif | ||
194 | } else if ((strcmp(argv[i],"-d")==0) || | ||
195 | (strcmp(argv[i],"--delete-bootloader")==0)) { | ||
196 | action = DELETE_BOOTLOADER; | ||
197 | i++; | ||
198 | } else if ((strcmp(argv[i],"-a")==0) || | ||
199 | (strcmp(argv[i],"--add-bootloader")==0)) { | ||
200 | action = ADD_BOOTLOADER; | ||
201 | type = FILETYPE_DOT_IPOD; | ||
202 | i++; | ||
203 | if (i == argc) { print_usage(); return 1; } | ||
204 | filename=argv[i]; | ||
205 | i++; | ||
206 | } else if ((strcmp(argv[i],"-ab")==0) || | ||
207 | (strcmp(argv[i],"--add-bootloader-bin")==0)) { | ||
208 | action = ADD_BOOTLOADER; | ||
209 | type = FILETYPE_DOT_BIN; | ||
210 | i++; | ||
211 | if (i == argc) { print_usage(); return 1; } | ||
212 | filename=argv[i]; | ||
213 | i++; | ||
214 | } else if ((strcmp(argv[i],"-rf")==0) || | ||
215 | (strcmp(argv[i],"--read-firmware")==0)) { | ||
216 | action = READ_FIRMWARE; | ||
217 | i++; | ||
218 | if (i == argc) { print_usage(); return 1; } | ||
219 | filename=argv[i]; | ||
220 | i++; | ||
221 | } else if ((strcmp(argv[i],"-wf")==0) || | ||
222 | (strcmp(argv[i],"--write-firmware")==0)) { | ||
223 | action = WRITE_FIRMWARE; | ||
224 | type = FILETYPE_DOT_IPOD; | ||
225 | i++; | ||
226 | if (i == argc) { print_usage(); return 1; } | ||
227 | filename=argv[i]; | ||
228 | i++; | ||
229 | } else if ((strcmp(argv[i],"-wfb")==0) || | ||
230 | (strcmp(argv[i],"--write-firmware-bin")==0)) { | ||
231 | action = WRITE_FIRMWARE; | ||
232 | type = FILETYPE_DOT_BIN; | ||
233 | i++; | ||
234 | if (i == argc) { print_usage(); return 1; } | ||
235 | filename=argv[i]; | ||
236 | i++; | ||
237 | } else if ((strcmp(argv[i],"-r")==0) || | ||
238 | (strcmp(argv[i],"--read-partition")==0)) { | ||
239 | action = READ_PARTITION; | ||
240 | i++; | ||
241 | if (i == argc) { print_usage(); return 1; } | ||
242 | filename=argv[i]; | ||
243 | i++; | ||
244 | } else if ((strcmp(argv[i],"-w")==0) || | ||
245 | (strcmp(argv[i],"--write-partition")==0)) { | ||
246 | action = WRITE_PARTITION; | ||
247 | i++; | ||
248 | if (i == argc) { print_usage(); return 1; } | ||
249 | filename=argv[i]; | ||
250 | i++; | ||
251 | } else if ((strcmp(argv[i],"-v")==0) || | ||
252 | (strcmp(argv[i],"--verbose")==0)) { | ||
253 | verbose++; | ||
254 | i++; | ||
255 | } else { | ||
256 | print_usage(); return 1; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | if (ipod.diskname[0]==0) { | ||
261 | print_usage(); | ||
262 | return 1; | ||
263 | } | ||
264 | |||
265 | if (ipod_open(&ipod, 0) < 0) { | ||
266 | return 1; | ||
267 | } | ||
268 | |||
269 | fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname); | ||
270 | fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size); | ||
271 | |||
272 | if (read_partinfo(&ipod,0) < 0) { | ||
273 | return 2; | ||
274 | } | ||
275 | |||
276 | display_partinfo(&ipod); | ||
277 | |||
278 | if (ipod.pinfo[0].start==0) { | ||
279 | fprintf(stderr,"[ERR] No partition 0 on disk:\n"); | ||
280 | display_partinfo(&ipod); | ||
281 | return 3; | ||
282 | } | ||
283 | |||
284 | read_directory(&ipod); | ||
285 | |||
286 | if (ipod.nimages <= 0) { | ||
287 | fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages); | ||
288 | return 1; | ||
289 | } | ||
290 | |||
291 | if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0) { | ||
292 | fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n", | ||
293 | ipod.ipod_directory[0].vers); | ||
294 | return -1; | ||
295 | } | ||
296 | |||
297 | printf("[INFO] Ipod model: %s (\"%s\")\n",ipod.modelstr, | ||
298 | ipod.macpod ? "macpod" : "winpod"); | ||
299 | |||
300 | if (ipod.macpod) { | ||
301 | print_macpod_warning(); | ||
302 | } | ||
303 | |||
304 | if (action==LIST_IMAGES) { | ||
305 | list_images(&ipod); | ||
306 | #ifdef WITH_BOOTOBJS | ||
307 | } else if (action==INTERACTIVE) { | ||
308 | |||
309 | printf("Do you wish to install the rockbox bootloader? (y/n) :"); | ||
310 | if (fgets(yesno,4,stdin)) { | ||
311 | if (yesno[0]=='y') { | ||
312 | if (ipod_reopen_rw(&ipod) < 0) { | ||
313 | return 5; | ||
314 | } | ||
315 | |||
316 | if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { | ||
317 | fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); | ||
318 | } else { | ||
319 | fprintf(stderr,"[ERR] --install failed.\n"); | ||
320 | } | ||
321 | } | ||
322 | } | ||
323 | #endif | ||
324 | } else if (action==DELETE_BOOTLOADER) { | ||
325 | if (ipod_reopen_rw(&ipod) < 0) { | ||
326 | return 5; | ||
327 | } | ||
328 | |||
329 | if (ipod.ipod_directory[0].entryOffset==0) { | ||
330 | fprintf(stderr,"[ERR] No bootloader detected.\n"); | ||
331 | } else { | ||
332 | if (delete_bootloader(&ipod)==0) { | ||
333 | fprintf(stderr,"[INFO] Bootloader removed.\n"); | ||
334 | } else { | ||
335 | fprintf(stderr,"[ERR] --delete-bootloader failed.\n"); | ||
336 | } | ||
337 | } | ||
338 | } else if (action==ADD_BOOTLOADER) { | ||
339 | if (ipod_reopen_rw(&ipod) < 0) { | ||
340 | return 5; | ||
341 | } | ||
342 | |||
343 | if (add_bootloader(&ipod, filename, type)==0) { | ||
344 | fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename); | ||
345 | } else { | ||
346 | fprintf(stderr,"[ERR] --add-bootloader failed.\n"); | ||
347 | } | ||
348 | #ifdef WITH_BOOTOBJS | ||
349 | } else if (action==INSTALL) { | ||
350 | if (ipod_reopen_rw(&ipod) < 0) { | ||
351 | return 5; | ||
352 | } | ||
353 | |||
354 | if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { | ||
355 | fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); | ||
356 | } else { | ||
357 | fprintf(stderr,"[ERR] --install failed.\n"); | ||
358 | } | ||
359 | #endif | ||
360 | } else if (action==WRITE_FIRMWARE) { | ||
361 | if (ipod_reopen_rw(&ipod) < 0) { | ||
362 | return 5; | ||
363 | } | ||
364 | |||
365 | if (write_firmware(&ipod, filename,type)==0) { | ||
366 | fprintf(stderr,"[INFO] Firmware %s written to device.\n",filename); | ||
367 | } else { | ||
368 | fprintf(stderr,"[ERR] --write-firmware failed.\n"); | ||
369 | } | ||
370 | } else if (action==READ_FIRMWARE) { | ||
371 | if (read_firmware(&ipod, filename)==0) { | ||
372 | fprintf(stderr,"[INFO] Firmware read to file %s.\n",filename); | ||
373 | } else { | ||
374 | fprintf(stderr,"[ERR] --read-firmware failed.\n"); | ||
375 | } | ||
376 | } else if (action==READ_PARTITION) { | ||
377 | outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); | ||
378 | if (outfile < 0) { | ||
379 | perror(filename); | ||
380 | return 4; | ||
381 | } | ||
382 | |||
383 | if (read_partition(&ipod, outfile) < 0) { | ||
384 | fprintf(stderr,"[ERR] --read-partition failed.\n"); | ||
385 | } else { | ||
386 | fprintf(stderr,"[INFO] Partition extracted to %s.\n",filename); | ||
387 | } | ||
388 | close(outfile); | ||
389 | } else if (action==WRITE_PARTITION) { | ||
390 | if (ipod_reopen_rw(&ipod) < 0) { | ||
391 | return 5; | ||
392 | } | ||
393 | |||
394 | infile = open(filename,O_RDONLY|O_BINARY); | ||
395 | if (infile < 0) { | ||
396 | perror(filename); | ||
397 | return 2; | ||
398 | } | ||
399 | |||
400 | /* Check filesize is <= partition size */ | ||
401 | inputsize=filesize(infile); | ||
402 | if (inputsize > 0) { | ||
403 | if (inputsize <= (ipod.pinfo[0].size*ipod.sector_size)) { | ||
404 | fprintf(stderr,"[INFO] Input file is %u bytes\n",inputsize); | ||
405 | if (write_partition(&ipod,infile) < 0) { | ||
406 | fprintf(stderr,"[ERR] --write-partition failed.\n"); | ||
407 | } else { | ||
408 | fprintf(stderr,"[INFO] %s restored to partition\n",filename); | ||
409 | } | ||
410 | } else { | ||
411 | fprintf(stderr,"[ERR] File is too large for firmware partition, aborting.\n"); | ||
412 | } | ||
413 | } | ||
414 | |||
415 | close(infile); | ||
416 | } | ||
417 | |||
418 | ipod_close(&ipod); | ||
419 | |||
420 | #ifdef WITH_BOOTOBJS | ||
421 | if (action==INTERACTIVE) { | ||
422 | printf("Press ENTER to exit ipodpatcher :"); | ||
423 | fgets(yesno,4,stdin); | ||
424 | } | ||
425 | #endif | ||
426 | |||
427 | |||
428 | return 0; | ||
429 | } | ||