summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2020-09-25 22:45:05 +0200
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2020-09-27 09:28:10 +0200
commitaf76fa3e7a321b303b0d219ed95578079bbc8a24 (patch)
tree202cb19de683700abff7b4a816599001871821a0
parent3806f57ed8a220358e701b4f29703dd3820ac686 (diff)
downloadrockbox-af76fa3e7a321b303b0d219ed95578079bbc8a24.tar.gz
rockbox-af76fa3e7a321b303b0d219ed95578079bbc8a24.zip
ipodpatcher: Rework exit codes.
- #define all exit codes used so they can be checked more easily. - Use exit codes to indicate more error states. Change-Id: I052962e3457a7cd5eca8b70256889d25b1b4b3aa
-rw-r--r--rbutil/ipodpatcher/ipodpatcher.h20
-rw-r--r--rbutil/ipodpatcher/main.c75
2 files changed, 57 insertions, 38 deletions
diff --git a/rbutil/ipodpatcher/ipodpatcher.h b/rbutil/ipodpatcher/ipodpatcher.h
index 6f0498a4e0..2cd2331666 100644
--- a/rbutil/ipodpatcher/ipodpatcher.h
+++ b/rbutil/ipodpatcher/ipodpatcher.h
@@ -19,8 +19,8 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#ifndef _IPODPATCHER_H 22#ifndef IPODPATCHER_H
23#define _IPODPATCHER_H 23#define IPODPATCHER_H
24 24
25#ifdef __cplusplus 25#ifdef __cplusplus
26extern "C" { 26extern "C" {
@@ -28,6 +28,22 @@ extern "C" {
28 28
29#include "ipodio.h" 29#include "ipodio.h"
30 30
31/* exit codes */
32#define IPOD_OK 0
33#define IPOD_WRONG_ARGUMENTS 1
34#define IPOD_OPEN_INFILE_FAILED 2
35#define IPOD_PARTITION_ERROR 3
36#define IPOD_OPEN_OUTFILE_FAILED 4
37#define IPOD_CANNOT_REOPEN 5
38#define IPOD_ACCESS_DENIED 10
39#define IPOD_NOT_FOUND 11
40#define IPOD_WRONG_DEVICE_COUNT 12
41#define IPOD_IMAGE_ERROR 13
42#define IPOD_DUMP_FAILED 14
43#define IPOD_MULTIPLE_DEVICES 15
44#define IPOD_WRONG_TYPE 16
45#define IPOD_UNKNOWN_FW_VERSION -1
46
31/* Size of buffer for disk I/O - 8MB is large enough for any version 47/* Size of buffer for disk I/O - 8MB is large enough for any version
32 of the Apple firmware, but not the Nano's RSRC image. */ 48 of the Apple firmware, but not the Nano's RSRC image. */
33#define BUFFER_SIZE 8*1024*1024 49#define BUFFER_SIZE 8*1024*1024
diff --git a/rbutil/ipodpatcher/main.c b/rbutil/ipodpatcher/main.c
index 634bb250c0..7b0a909178 100644
--- a/rbutil/ipodpatcher/main.c
+++ b/rbutil/ipodpatcher/main.c
@@ -161,7 +161,7 @@ int main(int argc, char* argv[])
161 161
162 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) { 162 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
163 print_usage(); 163 print_usage();
164 return 1; 164 return IPOD_OK;
165 } 165 }
166 166
167 if (ipod_alloc_buffer(&ipod,BUFFER_SIZE) < 0) { 167 if (ipod_alloc_buffer(&ipod,BUFFER_SIZE) < 0) {
@@ -171,7 +171,7 @@ int main(int argc, char* argv[])
171 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) { 171 if ((argc > 1) && (strcmp(argv[1],"--scan")==0)) {
172 if (ipod_scan(&ipod) == 0) 172 if (ipod_scan(&ipod) == 0)
173 fprintf(stderr,"[ERR] No ipods found.\n"); 173 fprintf(stderr,"[ERR] No ipods found.\n");
174 return 0; 174 return IPOD_NOT_FOUND;
175 } 175 }
176 176
177 /* If the first parameter doesn't start with -, then we interpret it as a device */ 177 /* If the first parameter doesn't start with -, then we interpret it as a device */
@@ -200,6 +200,9 @@ int main(int argc, char* argv[])
200 } else if (n > 1) { 200 } else if (n > 1) {
201 fprintf(stderr,"[ERR] %d ipods found, aborting\n",n); 201 fprintf(stderr,"[ERR] %d ipods found, aborting\n",n);
202 fprintf(stderr,"[ERR] Please connect only one ipod and re-run ipodpatcher.\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;
203 } 206 }
204 207
205 if (n != 1) { 208 if (n != 1) {
@@ -209,7 +212,7 @@ int main(int argc, char* argv[])
209 fgets(yesno,4,stdin); 212 fgets(yesno,4,stdin);
210 } 213 }
211#endif 214#endif
212 return 0; 215 return IPOD_NOT_FOUND;
213 } 216 }
214 217
215 i = 1; 218 i = 1;
@@ -239,7 +242,7 @@ int main(int argc, char* argv[])
239 action = ADD_BOOTLOADER; 242 action = ADD_BOOTLOADER;
240 type = FILETYPE_DOT_IPOD; 243 type = FILETYPE_DOT_IPOD;
241 i++; 244 i++;
242 if (i == argc) { print_usage(); return 1; } 245 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
243 filename=argv[i]; 246 filename=argv[i];
244 i++; 247 i++;
245 } else if ((strcmp(argv[i],"-ab")==0) || 248 } else if ((strcmp(argv[i],"-ab")==0) ||
@@ -247,7 +250,7 @@ int main(int argc, char* argv[])
247 action = ADD_BOOTLOADER; 250 action = ADD_BOOTLOADER;
248 type = FILETYPE_DOT_BIN; 251 type = FILETYPE_DOT_BIN;
249 i++; 252 i++;
250 if (i == argc) { print_usage(); return 1; } 253 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
251 filename=argv[i]; 254 filename=argv[i];
252 i++; 255 i++;
253 } else if ((strcmp(argv[i],"-rf")==0) || 256 } else if ((strcmp(argv[i],"-rf")==0) ||
@@ -255,7 +258,7 @@ int main(int argc, char* argv[])
255 action = READ_FIRMWARE; 258 action = READ_FIRMWARE;
256 type = FILETYPE_DOT_IPOD; 259 type = FILETYPE_DOT_IPOD;
257 i++; 260 i++;
258 if (i == argc) { print_usage(); return 1; } 261 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
259 filename=argv[i]; 262 filename=argv[i];
260 i++; 263 i++;
261 } else if ((strcmp(argv[i],"-rfb")==0) || 264 } else if ((strcmp(argv[i],"-rfb")==0) ||
@@ -263,7 +266,7 @@ int main(int argc, char* argv[])
263 action = READ_FIRMWARE; 266 action = READ_FIRMWARE;
264 type = FILETYPE_DOT_BIN; 267 type = FILETYPE_DOT_BIN;
265 i++; 268 i++;
266 if (i == argc) { print_usage(); return 1; } 269 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
267 filename=argv[i]; 270 filename=argv[i];
268 i++; 271 i++;
269#ifdef WITH_BOOTOBJS 272#ifdef WITH_BOOTOBJS
@@ -279,7 +282,7 @@ int main(int argc, char* argv[])
279 action = WRITE_FIRMWARE; 282 action = WRITE_FIRMWARE;
280 type = FILETYPE_DOT_IPOD; 283 type = FILETYPE_DOT_IPOD;
281 i++; 284 i++;
282 if (i == argc) { print_usage(); return 1; } 285 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
283 filename=argv[i]; 286 filename=argv[i];
284 i++; 287 i++;
285 } else if ((strcmp(argv[i],"-wfb")==0) || 288 } else if ((strcmp(argv[i],"-wfb")==0) ||
@@ -287,21 +290,21 @@ int main(int argc, char* argv[])
287 action = WRITE_FIRMWARE; 290 action = WRITE_FIRMWARE;
288 type = FILETYPE_DOT_BIN; 291 type = FILETYPE_DOT_BIN;
289 i++; 292 i++;
290 if (i == argc) { print_usage(); return 1; } 293 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
291 filename=argv[i]; 294 filename=argv[i];
292 i++; 295 i++;
293 } else if ((strcmp(argv[i],"-r")==0) || 296 } else if ((strcmp(argv[i],"-r")==0) ||
294 (strcmp(argv[i],"--read-partition")==0)) { 297 (strcmp(argv[i],"--read-partition")==0)) {
295 action = READ_PARTITION; 298 action = READ_PARTITION;
296 i++; 299 i++;
297 if (i == argc) { print_usage(); return 1; } 300 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
298 filename=argv[i]; 301 filename=argv[i];
299 i++; 302 i++;
300 } else if ((strcmp(argv[i],"-w")==0) || 303 } else if ((strcmp(argv[i],"-w")==0) ||
301 (strcmp(argv[i],"--write-partition")==0)) { 304 (strcmp(argv[i],"--write-partition")==0)) {
302 action = WRITE_PARTITION; 305 action = WRITE_PARTITION;
303 i++; 306 i++;
304 if (i == argc) { print_usage(); return 1; } 307 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
305 filename=argv[i]; 308 filename=argv[i];
306 i++; 309 i++;
307 } else if ((strcmp(argv[i],"-v")==0) || 310 } else if ((strcmp(argv[i],"-v")==0) ||
@@ -315,20 +318,20 @@ int main(int argc, char* argv[])
315 } else if (strcmp(argv[i],"--read-aupd")==0) { 318 } else if (strcmp(argv[i],"--read-aupd")==0) {
316 action = READ_AUPD; 319 action = READ_AUPD;
317 i++; 320 i++;
318 if (i == argc) { print_usage(); return 1; } 321 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
319 filename=argv[i]; 322 filename=argv[i];
320 i++; 323 i++;
321 } else if (strcmp(argv[i],"--write-aupd")==0) { 324 } else if (strcmp(argv[i],"--write-aupd")==0) {
322 action = WRITE_AUPD; 325 action = WRITE_AUPD;
323 i++; 326 i++;
324 if (i == argc) { print_usage(); return 1; } 327 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
325 filename=argv[i]; 328 filename=argv[i];
326 i++; 329 i++;
327 } else if ((strcmp(argv[i],"-x")==0) || 330 } else if ((strcmp(argv[i],"-x")==0) ||
328 (strcmp(argv[i],"--dump-xml")==0)) { 331 (strcmp(argv[i],"--dump-xml")==0)) {
329 action = DUMP_XML; 332 action = DUMP_XML;
330 i++; 333 i++;
331 if (i == argc) { print_usage(); return 1; } 334 if (i == argc) { print_usage(); return IPOD_WRONG_ARGUMENTS; }
332 filename=argv[i]; 335 filename=argv[i];
333 i++; 336 i++;
334 } else if ((strcmp(argv[i],"-c")==0) || 337 } else if ((strcmp(argv[i],"-c")==0) ||
@@ -336,7 +339,7 @@ int main(int argc, char* argv[])
336 action = CONVERT_TO_FAT32; 339 action = CONVERT_TO_FAT32;
337 i++; 340 i++;
338 } else { 341 } else {
339 print_usage(); return 1; 342 print_usage(); return IPOD_WRONG_ARGUMENTS;
340 } 343 }
341 } 344 }
342 345
@@ -346,14 +349,14 @@ int main(int argc, char* argv[])
346 } 349 }
347 350
348 if (ipod_open(&ipod, 0) < 0) { 351 if (ipod_open(&ipod, 0) < 0) {
349 return 1; 352 return IPOD_ACCESS_DENIED;
350 } 353 }
351 354
352 fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname); 355 fprintf(stderr,"[INFO] Reading partition table from %s\n",ipod.diskname);
353 fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size); 356 fprintf(stderr,"[INFO] Sector size is %d bytes\n",ipod.sector_size);
354 357
355 if (read_partinfo(&ipod,0) < 0) { 358 if (read_partinfo(&ipod,0) < 0) {
356 return 2; 359 return IPOD_PARTITION_ERROR;
357 } 360 }
358 361
359 display_partinfo(&ipod); 362 display_partinfo(&ipod);
@@ -361,26 +364,26 @@ int main(int argc, char* argv[])
361 if (ipod.pinfo[0].start==0) { 364 if (ipod.pinfo[0].start==0) {
362 fprintf(stderr,"[ERR] No partition 0 on disk:\n"); 365 fprintf(stderr,"[ERR] No partition 0 on disk:\n");
363 display_partinfo(&ipod); 366 display_partinfo(&ipod);
364 return 3; 367 return IPOD_PARTITION_ERROR;
365 } 368 }
366 369
367 read_directory(&ipod); 370 read_directory(&ipod);
368 371
369 if (ipod.nimages <= 0) { 372 if (ipod.nimages <= 0) {
370 fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages); 373 fprintf(stderr,"[ERR] Failed to read firmware directory - nimages=%d\n",ipod.nimages);
371 return 1; 374 return IPOD_IMAGE_ERROR;
372 } 375 }
373 376
374 if (getmodel(&ipod,(ipod.ipod_directory[ipod.ososimage].vers>>8)) < 0) { 377 if (getmodel(&ipod,(ipod.ipod_directory[ipod.ososimage].vers>>8)) < 0) {
375 fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n", 378 fprintf(stderr,"[ERR] Unknown version number in firmware (%08x)\n",
376 ipod.ipod_directory[ipod.ososimage].vers); 379 ipod.ipod_directory[ipod.ososimage].vers);
377 return -1; 380 return IPOD_UNKNOWN_FW_VERSION;
378 } 381 }
379 382
380#ifdef __WIN32__ 383#ifdef __WIN32__
381 /* Windows requires the ipod in R/W mode for SCSI Inquiry */ 384 /* Windows requires the ipod in R/W mode for SCSI Inquiry */
382 if (ipod_reopen_rw(&ipod) < 0) { 385 if (ipod_reopen_rw(&ipod) < 0) {
383 return 5; 386 return IPOD_CANNOT_REOPEN;
384 } 387 }
385#endif 388#endif
386 389
@@ -412,7 +415,7 @@ int main(int argc, char* argv[])
412 if (fgets(yesno,4,stdin)) { 415 if (fgets(yesno,4,stdin)) {
413 if (yesno[0]=='i') { 416 if (yesno[0]=='i') {
414 if (ipod_reopen_rw(&ipod) < 0) { 417 if (ipod_reopen_rw(&ipod) < 0) {
415 return 5; 418 return IPOD_CANNOT_REOPEN;
416 } 419 }
417 420
418 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { 421 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
@@ -422,7 +425,7 @@ int main(int argc, char* argv[])
422 } 425 }
423 } else if (yesno[0]=='u') { 426 } else if (yesno[0]=='u') {
424 if (ipod_reopen_rw(&ipod) < 0) { 427 if (ipod_reopen_rw(&ipod) < 0) {
425 return 5; 428 return IPOD_CANNOT_REOPEN;
426 } 429 }
427 430
428 if (delete_bootloader(&ipod)==0) { 431 if (delete_bootloader(&ipod)==0) {
@@ -435,7 +438,7 @@ int main(int argc, char* argv[])
435#endif 438#endif
436 } else if (action==DELETE_BOOTLOADER) { 439 } else if (action==DELETE_BOOTLOADER) {
437 if (ipod_reopen_rw(&ipod) < 0) { 440 if (ipod_reopen_rw(&ipod) < 0) {
438 return 5; 441 return IPOD_CANNOT_REOPEN;
439 } 442 }
440 443
441 if (ipod.ipod_directory[0].entryOffset==0) { 444 if (ipod.ipod_directory[0].entryOffset==0) {
@@ -449,7 +452,7 @@ int main(int argc, char* argv[])
449 } 452 }
450 } else if (action==ADD_BOOTLOADER) { 453 } else if (action==ADD_BOOTLOADER) {
451 if (ipod_reopen_rw(&ipod) < 0) { 454 if (ipod_reopen_rw(&ipod) < 0) {
452 return 5; 455 return IPOD_CANNOT_REOPEN;
453 } 456 }
454 457
455 if (add_bootloader(&ipod, filename, type)==0) { 458 if (add_bootloader(&ipod, filename, type)==0) {
@@ -460,7 +463,7 @@ int main(int argc, char* argv[])
460#ifdef WITH_BOOTOBJS 463#ifdef WITH_BOOTOBJS
461 } else if (action==INSTALL) { 464 } else if (action==INSTALL) {
462 if (ipod_reopen_rw(&ipod) < 0) { 465 if (ipod_reopen_rw(&ipod) < 0) {
463 return 5; 466 return IPOD_CANNOT_REOPEN;
464 } 467 }
465 468
466 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) { 469 if (add_bootloader(&ipod, NULL, FILETYPE_INTERNAL)==0) {
@@ -471,7 +474,7 @@ int main(int argc, char* argv[])
471#endif 474#endif
472 } else if (action==WRITE_FIRMWARE) { 475 } else if (action==WRITE_FIRMWARE) {
473 if (ipod_reopen_rw(&ipod) < 0) { 476 if (ipod_reopen_rw(&ipod) < 0) {
474 return 5; 477 return IPOD_CANNOT_REOPEN;
475 } 478 }
476 479
477 if (write_firmware(&ipod, filename,type)==0) { 480 if (write_firmware(&ipod, filename,type)==0) {
@@ -493,7 +496,7 @@ int main(int argc, char* argv[])
493 } 496 }
494 } else if (action==WRITE_AUPD) { 497 } else if (action==WRITE_AUPD) {
495 if (ipod_reopen_rw(&ipod) < 0) { 498 if (ipod_reopen_rw(&ipod) < 0) {
496 return 5; 499 return IPOD_CANNOT_REOPEN;
497 } 500 }
498 501
499 if (write_aupd(&ipod, filename)==0) { 502 if (write_aupd(&ipod, filename)==0) {
@@ -504,13 +507,13 @@ int main(int argc, char* argv[])
504 } else if (action==DUMP_XML) { 507 } else if (action==DUMP_XML) {
505 if (ipod.xmlinfo == NULL) { 508 if (ipod.xmlinfo == NULL) {
506 fprintf(stderr,"[ERR] No XML to write\n"); 509 fprintf(stderr,"[ERR] No XML to write\n");
507 return 1; 510 return IPOD_DUMP_FAILED;
508 } 511 }
509 512
510 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); 513 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
511 if (outfile < 0) { 514 if (outfile < 0) {
512 perror(filename); 515 perror(filename);
513 return 4; 516 return IPOD_OPEN_OUTFILE_FAILED;
514 } 517 }
515 518
516 if (write(outfile, ipod.xmlinfo, ipod.xmlinfo_len) < 0) { 519 if (write(outfile, ipod.xmlinfo, ipod.xmlinfo_len) < 0) {
@@ -523,7 +526,7 @@ int main(int argc, char* argv[])
523 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE); 526 outfile = open(filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IREAD|S_IWRITE);
524 if (outfile < 0) { 527 if (outfile < 0) {
525 perror(filename); 528 perror(filename);
526 return 4; 529 return IPOD_OPEN_OUTFILE_FAILED;
527 } 530 }
528 531
529 if (read_partition(&ipod, outfile) < 0) { 532 if (read_partition(&ipod, outfile) < 0) {
@@ -534,13 +537,13 @@ int main(int argc, char* argv[])
534 close(outfile); 537 close(outfile);
535 } else if (action==WRITE_PARTITION) { 538 } else if (action==WRITE_PARTITION) {
536 if (ipod_reopen_rw(&ipod) < 0) { 539 if (ipod_reopen_rw(&ipod) < 0) {
537 return 5; 540 return IPOD_CANNOT_REOPEN;
538 } 541 }
539 542
540 infile = open(filename,O_RDONLY|O_BINARY); 543 infile = open(filename,O_RDONLY|O_BINARY);
541 if (infile < 0) { 544 if (infile < 0) {
542 perror(filename); 545 perror(filename);
543 return 2; 546 return IPOD_OPEN_INFILE_FAILED;
544 } 547 }
545 548
546 /* Check filesize is <= partition size */ 549 /* Check filesize is <= partition size */
@@ -567,7 +570,7 @@ int main(int argc, char* argv[])
567 if (fgets(yesno,4,stdin)) { 570 if (fgets(yesno,4,stdin)) {
568 if (yesno[0]=='y') { 571 if (yesno[0]=='y') {
569 if (ipod_reopen_rw(&ipod) < 0) { 572 if (ipod_reopen_rw(&ipod) < 0) {
570 return 5; 573 return IPOD_CANNOT_REOPEN;
571 } 574 }
572 575
573 if (format_partition(&ipod,1) < 0) { 576 if (format_partition(&ipod,1) < 0) {
@@ -588,7 +591,7 @@ int main(int argc, char* argv[])
588 if (fgets(yesno,4,stdin)) { 591 if (fgets(yesno,4,stdin)) {
589 if (yesno[0]=='y') { 592 if (yesno[0]=='y') {
590 if (ipod_reopen_rw(&ipod) < 0) { 593 if (ipod_reopen_rw(&ipod) < 0) {
591 return 5; 594 return IPOD_CANNOT_REOPEN;
592 } 595 }
593 596
594 if (write_dos_partition_table(&ipod) < 0) { 597 if (write_dos_partition_table(&ipod) < 0) {
@@ -615,5 +618,5 @@ int main(int argc, char* argv[])
615#endif 618#endif
616 619
617 ipod_dealloc_buffer(&ipod); 620 ipod_dealloc_buffer(&ipod);
618 return 0; 621 return IPOD_OK;
619} 622}