summaryrefslogtreecommitdiff
path: root/tools/scramble.c
blob: 3b9497186d5e940b468b80e5126dbe33abf5721e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 - 2007 by Björn Stenberg
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "iriver.h"
#include "gigabeat.h"
#include "gigabeats.h"
#include "mi4.h"
#include "telechips.h"
#include "creative.h"
#include "iaudio_bl_flash.h"
#include "rkw.h"

static int iaudio_encode(char *iname, char *oname, char *idstring);
static int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc);
static int ccpmp_encode(char *iname, char *oname);

void short2le(unsigned short val, unsigned char* addr)
{
    addr[0] = val & 0xFF;
    addr[1] = (val >> 8) & 0xff;
}

unsigned int le2int(unsigned char* buf)
{
   unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];

   return res;
}

void int2le(unsigned int val, unsigned char* addr)
{
    addr[0] = val & 0xFF;
    addr[1] = (val >> 8) & 0xff;
    addr[2] = (val >> 16) & 0xff;
    addr[3] = (val >> 24) & 0xff;
}

void int2be(unsigned int val, unsigned char* addr)
{
    addr[0] = (val >> 24) & 0xff;
    addr[1] = (val >> 16) & 0xff;
    addr[2] = (val >> 8) & 0xff;
    addr[3] = val & 0xFF;
}

void short2be(unsigned short val, unsigned char* addr)
{
    addr[0] = (val >> 8) & 0xff;
    addr[1] = val & 0xFF;
}

void usage(void)
{
    printf("usage: scramble [options] <input file> <output file>\n");
    printf("options:\n"
           "\t-neo    SSI Neo format\n"
           "\t-iriver iRiver format\n"
           "\t-iaudiox5 iAudio X5 format\n"
           "\t-iaudiox5v iAudio X5V format\n"
           "\t-iaudiom5 iAudio M5 format\n"
           "\t-iaudiom3 iAudio M3 format\n");
    printf("\t-ipod3g ipod firmware partition format (3rd Gen)\n"
           "\t-ipod4g ipod firmware partition format (4th Gen, Mini, Nano, Photo/Color)\n"
           "\t-ipod5g ipod firmware partition format (5th Gen - aka Video)\n"
           "\t-creative=X [-no-ciff] Creative firmware structure format\n"
           "\t            (X values: zvm, zvm60, zenvision, zenv, zen,\n"
           "\t             zenxfi, zenmozaic)\n");
    printf("\t-gigabeat Toshiba Gigabeat F/X format\n"
           "\t-gigabeats Toshiba Gigabeat S format\n"
           "\t-mi4v2  PortalPlayer .mi4 format (revision 010201)\n"
           "\t-mi4v3  PortalPlayer .mi4 format (revision 010301)\n"
           "\t-mi4r   Sandisk Rhapsody .mi4 format\n"
           "\t        All mi4 options take two optional arguments:\n");
    printf("\t        -model=XXXX   where XXXX is the model id string\n"
           "\t        -type=XXXX    where XXXX is a string indicating the \n"
           "\t                      type of binary, eg. RBOS, RBBL\n"
           "\t-tcc=X  Telechips generic firmware format (X values: sum, crc)\n"
           "\t-rkw Rockchip RKW format\n"
           "\t-add=X  Rockbox generic \"add-up\" checksum format\n"
           "\t        (X values: h100, h120, h140, h300, ipco, nano, ipvd, mn2g\n"
           "\t                   ip3g, ip4g, mini, iax5, iam5, iam3, h10, h10_5gb,\n");
    printf("\t                   tpj2, c200, e200, giga, gigs, m100, m500, d2,\n"
           "\t                   9200, 1630, 6330, ldax, m200, c100, clip, e2v2,\n"
           "\t                   m2v4, fuze, c2v2, clv2, y820, y920, y925, x747,\n"
           "\t                   747p, x777, nn2g, m244, cli+, fuz2, hd20, hd30,\n"
           "\t                   ip6g, rk27, clzp, zxf2, zxf3, fuz+, e370, e360,\n"
           "\t                   zxfi, zmoz, zen, zenv, ypz5, zxfs, e450, e460,\n"
           "\t                   e470, e580, a10, a20, a860, s750, e350, xdx3)\n");
    printf("\nNo option results in nothing being done.\n");

    exit(1);
}

int main (int argc, char** argv)
{
    unsigned long length,i;
    unsigned char *inbuf,*outbuf;
    unsigned short crc=0;
    unsigned long chksum=0; /* 32 bit checksum */
    unsigned char header[24];
    char *iname = argv[1];
    char *oname = argv[2];
    int headerlen = 6;
    FILE* file;
    int version=0;
    unsigned long modelnum;
    char modelname[5];
    enum { none, tcc_sum, tcc_crc, rkw, add } method = none;
    bool creative_enable_ciff;

    if (argc < 3) {
        usage();
    }

    else if(!strcmp(argv[1], "-neo")) {
        headerlen = 17;
        iname = argv[2];
        oname = argv[3];
        method = none;
    }
    else if(!strncmp(argv[1], "-tcc=", 4)) {
        headerlen = 0;
        iname = argv[2];
        oname = argv[3];

        if(!strcmp(&argv[1][5], "sum"))
            method = tcc_sum;
        else if(!strcmp(&argv[1][5], "crc"))
            method = tcc_crc;
        else {
            fprintf(stderr, "unsupported TCC method: %s\n", &argv[1][5]);
            return 2;
        }
    }
    else if(!strncmp(argv[1], "-rkw", 4)) {
        iname = argv[3];
        oname = argv[4];
        modelnum = 0;

        if(!strncmp(argv[2], "-modelnum=", 10)) {
            modelnum = atoi(&argv[2][10]);
        }

        if (!modelnum)
        {
            modelnum = 73; /* rk27generic */
            fprintf(stderr, "modelnum not supplied."
                            " using default value for rk27generic target\n");
        }

        return (rkw_encode(iname, oname, modelnum) != 0) ? -1 : 0;
    }
    else if(!strncmp(argv[1], "-add=", 5)) {
        iname = argv[2];
        oname = argv[3];
        method = add;

        if(!strcmp(&argv[1][5], "h120"))
            modelnum = 0;
        else if(!strcmp(&argv[1][5], "h140"))
            modelnum = 0; /* the same as the h120 */
        else if(!strcmp(&argv[1][5], "h100"))
            modelnum = 1;
        else if(!strcmp(&argv[1][5], "h300"))
            modelnum = 2;
        else if(!strcmp(&argv[1][5], "ipco"))
            modelnum = 3;
        else if(!strcmp(&argv[1][5], "nano"))
            modelnum = 4;
        else if(!strcmp(&argv[1][5], "ipvd"))
            modelnum = 5;
        else if(!strcmp(&argv[1][5], "fp7x"))
            modelnum = 6;
        else if(!strcmp(&argv[1][5], "ip3g"))
            modelnum = 7;
        else if(!strcmp(&argv[1][5], "ip4g"))
            modelnum = 8;
        else if(!strcmp(&argv[1][5], "mini"))
            modelnum = 9;
        else if(!strcmp(&argv[1][5], "iax5"))
            modelnum = 10;
        else if(!strcmp(&argv[1][5], "mn2g"))
            modelnum = 11;
        else if(!strcmp(&argv[1][5], "h10"))
            modelnum = 13;
        else if(!strcmp(&argv[1][5], "h10_5gb"))
            modelnum = 14;
        else if(!strcmp(&argv[1][5], "tpj2"))
            modelnum = 15;
        else if(!strcmp(&argv[1][5], "e200"))
            modelnum = 16;
        else if(!strcmp(&argv[1][5], "iam5"))
            modelnum = 17;
        else if(!strcmp(&argv[1][5], "giga"))
            modelnum = 18;
        else if(!strcmp(&argv[1][5], "1g2g"))
            modelnum = 19;
        else if(!strcmp(&argv[1][5], "c200"))
            modelnum = 20;
        else if(!strcmp(&argv[1][5], "gigs"))
            modelnum = 21;
        else if(!strcmp(&argv[1][5], "m500"))
            modelnum = 22;
        else if(!strcmp(&argv[1][5], "m100"))
            modelnum = 23;
        else if(!strcmp(&argv[1][5], "d2"))
            modelnum = 24;
        else if(!strcmp(&argv[1][5], "iam3"))
            modelnum = 25;
        else if (!strcmp(&argv[1][5], "m200"))
            modelnum = 29;
        else if(!strcmp(&argv[1][5], "c100")) 
            modelnum = 30;            
        else if(!strcmp(&argv[1][5], "1630")) /* Philips HDD1630 */
            modelnum = 31;
        else if (!strcmp(&argv[1][5], "i7"))
            modelnum = 32;
        else if (!strcmp(&argv[1][5], "ldax"))
            modelnum = 33;
        else if(!strcmp(&argv[1][5], "9200")) /* Philips SA9200 */
            modelnum = 34;
        else if (!strcmp(&argv[1][5], "clip"))
            modelnum = 40;
        else if (!strcmp(&argv[1][5], "e2v2"))
            modelnum = 41;
        else if (!strcmp(&argv[1][5], "m2v4"))
            modelnum = 42;
        else if (!strcmp(&argv[1][5], "fuze"))
            modelnum = 43;
        else if (!strcmp(&argv[1][5], "c2v2"))
            modelnum = 44;
        else if (!strcmp(&argv[1][5], "x747"))
            modelnum = 45;
        else if (!strcmp(&argv[1][5], "747p"))
            modelnum = 54;
        else if (!strcmp(&argv[1][5], "y820")) /* Samsung YH-820 */
            modelnum = 57;
        else if (!strcmp(&argv[1][5], "y920")) /* Samsung YH-920 */
            modelnum = 58;
        else if (!strcmp(&argv[1][5], "y925")) /* Samsung YH-925 */
            modelnum = 59;
        else if (!strcmp(&argv[1][5], "clv2")) /* Sansa Clipv2 */
            modelnum = 60;
        else if (!strcmp(&argv[1][5], "x777"))
            modelnum = 61;
        else if (!strcmp(&argv[1][5], "nn2g")) /* iPod Nano 2nd Gen */
            modelnum = 62;
        else if (!strcmp(&argv[1][5], "x767"))
            modelnum = 64;
        else if (!strcmp(&argv[1][5], "6330")) /* Philips HDD6330 */
            modelnum = 65;
        else if (!strcmp(&argv[1][5], "cli+")) /* Sansa Clip+ */
            modelnum = 66;
        else if (!strcmp(&argv[1][5], "v500")) /* Packard Bell Vibe 500 */
            modelnum = 67;
        else if (!strcmp(&argv[1][5], "fuz2")) /* Sansa Fuze v2 */
            modelnum = 68;
        else if (!strcmp(&argv[1][5], "m244"))
            modelnum = 131;
        else if (!strcmp(&argv[1][5], "hd20")) /* MPIO HD200 */
            modelnum = 69;
        else if (!strcmp(&argv[1][5], "hd30")) /* MPIO HD300 */
            modelnum = 70;
        else if (!strcmp(&argv[1][5], "ip6g")) /* iPod Classic/6G */
            modelnum = 71;
        else if (!strcmp(&argv[1][5], "fuz+")) /* Sansa Fuze+ */
            modelnum = 72;
        else if (!strcmp(&argv[1][5], "clzp")) /* Sansa Clip Zip */
            modelnum = 79;
        else if (!strcmp(&argv[1][5], "conn")) /* Sansa Connect */
            modelnum = 81;
        else if (!strcmp(&argv[1][5], "zxf2")) /* Creative Zen X-Fi2 */
            modelnum = 82;
        else if (!strcmp(&argv[1][5], "zxf3")) /* Creative Zen X-Fi3 */
            modelnum = 83;
        else if (!strcmp(&argv[1][5], "ypz5")) /* Samsung YP-Z5 */
            modelnum = 84;
        else if (!strcmp(&argv[1][5], "zenv")) /* Creative Zen V */
            modelnum = 85;
        else if (!strcmp(&argv[1][5], "zxfi")) /* Creative ZEN X-Fi */
            modelnum = 86;
        else if (!strcmp(&argv[1][5], "zmoz")) /* Creative ZEN Mozaic*/
            modelnum = 87;
        else if (!strcmp(&argv[1][5], "e370")) /* Sony NWZ-E370 series */
            modelnum = 88;
        else if (!strcmp(&argv[1][5], "e360")) /* Sony NWZ-E360 series */
            modelnum = 89;
        else if (!strcmp(&argv[1][5], "zen")) /* Creative ZEN */
            modelnum = 90;
        else if (!strcmp(&argv[1][5], "zxfs")) /* Creative ZEN X-Fi Style */
            modelnum = 94;
        else if (!strcmp(&argv[1][5], "xdx3")) /* xDuoo X3 */
            modelnum = 96;
        else if (!strcmp(&argv[1][5], "e450")) /* Sony NWZ-E450 series */
            modelnum = 100;
        else if (!strcmp(&argv[1][5], "e460")) /* Sony NWZ-E460 series */
            modelnum = 101;
        else if (!strcmp(&argv[1][5], "e580")) /* Sony NWZ-E580 series */
            modelnum = 102;
        else if (!strcmp(&argv[1][5], "e470")) /* Sony NWZ-E470 series */
            modelnum = 103;
        else if (!strcmp(&argv[1][5], "a10")) /* Sony NWZ-A10 series */
            modelnum = 104;
        else if (!strcmp(&argv[1][5], "a20")) /* Sony NW-A20 series */
            modelnum = 106;
        else if (!strcmp(&argv[1][5], "a860")) /* Sony NWZ-A860 series */
            modelnum = 107;
        else if (!strcmp(&argv[1][5], "s750")) /* Sony NWZ-S750 series */
            modelnum = 108;
        else if (!strcmp(&argv[1][5], "e350")) /* Sony NWZ-E350 series */
            modelnum = 109;
        else {
            fprintf(stderr, "unsupported model: %s\n", &argv[1][5]);
            return 2;
        }
        /* we store a 4-letter model name too, for humans */
        strncpy(modelname, &argv[1][5],4);
        modelname[4] = '\0'; /* to be sure we are null terminated */
        chksum = modelnum; /* start checksum calcs with this */
    }

    else if(!strcmp(argv[1], "-iriver")) {
        /* iRiver code dealt with in the iriver.c code */
        iname = argv[2];
        oname = argv[3];
        return (iriver_encode(iname, oname, FALSE) != 0) ? -1 : 0;
    }
    else if(!strcmp(argv[1], "-gigabeat")) {
        /* iRiver code dealt with in the iriver.c code */
        iname = argv[2];
        oname = argv[3];
        gigabeat_code(iname, oname);
        return 0;
    }
    else if(!strcmp(argv[1], "-gigabeats")) {
        iname = argv[2];
        oname = argv[3];
        return gigabeat_s_code(iname, oname);
    }
    else if(!strcmp(argv[1], "-iaudiox5")) {
        iname = argv[2];
        oname = argv[3];
        return iaudio_encode(iname, oname, "COWON_X5_FW");
    }
    else if(!strcmp(argv[1], "-iaudiox5v")) {
        iname = argv[2];
        oname = argv[3];
        return iaudio_encode(iname, oname, "COWON_X5V_FW");
    }
    else if(!strcmp(argv[1], "-iaudiom5")) {
        iname = argv[2];
        oname = argv[3];
        return iaudio_encode(iname, oname, "COWON_M5_FW");
    }
    else if(!strcmp(argv[1], "-iaudiom3")) {
        iname = argv[2];
        oname = argv[3];
        return iaudio_encode(iname, oname, "COWON_M3_FW");
    }
    else if(!strcmp(argv[1], "-ipod3g")) {
        iname = argv[2];
        oname = argv[3];
        return ipod_encode(iname, oname, 2, false); /* Firmware image v2 */
    }
    else if(!strcmp(argv[1], "-ipod4g")) {
        iname = argv[2];
        oname = argv[3];
        return ipod_encode(iname, oname, 3, false); /* Firmware image v3 */
    }
    else if(!strcmp(argv[1], "-ipod5g")) {
        iname = argv[2];
        oname = argv[3];
        return ipod_encode(iname, oname, 3, true);  /* Firmware image v3 */
    }
    else if(!strncmp(argv[1], "-creative=", 10)) {
        if(!strcmp(argv[2], "-no-ciff"))
        {
            creative_enable_ciff = false;
            iname = argv[3];
            oname = argv[4];
        }
        else
        {
            creative_enable_ciff = true;
            iname = argv[2];
            oname = argv[3];
        }
        if(!strcmp(&argv[1][10], "zvm"))
            return zvm_encode(iname, oname, ZENVISIONM, creative_enable_ciff);
        else if(!strcmp(&argv[1][10], "zvm60"))
            return zvm_encode(iname, oname, ZENVISIONM60, creative_enable_ciff);
        else if(!strcmp(&argv[1][10], "zenvision"))
            return zvm_encode(iname, oname, ZENVISION, creative_enable_ciff);
        else if(!strcmp(&argv[1][10], "zenv"))
            return zvm_encode(iname, oname, ZENV, creative_enable_ciff);
        else if(!strcmp(&argv[1][10], "zen"))
            return zvm_encode(iname, oname, ZEN, creative_enable_ciff);
        else if(!strcmp(&argv[1][10], "zenxfi"))
            return zvm_encode(iname, oname, ZENXFI, creative_enable_ciff);
        else if(!strcmp(&argv[1][10], "zenmozaic"))
            return zvm_encode(iname, oname, ZENMOZAIC, creative_enable_ciff);
        else
        {
            fprintf(stderr, "unsupported Creative device: %s\n", &argv[1][10]);
            return 2;
        }
    }
    else if(!strcmp(argv[1], "-ccpmp")) {
        iname = argv[2];
        oname = argv[3];
        return ccpmp_encode(iname, oname);
    }
    else if(!strncmp(argv[1], "-mi4", 4)) {
        int mi4magic;
        char model[4] = "";
        char type[4] = "";
        
        if(!strcmp(&argv[1][4], "v2")) {
            mi4magic = MI4_MAGIC_DEFAULT;
            version = 0x00010201;
        }
        else if(!strcmp(&argv[1][4], "v3")) {
            mi4magic = MI4_MAGIC_DEFAULT;
            version = 0x00010301;
        }
        else if(!strcmp(&argv[1][4], "r")) {
            mi4magic = MI4_MAGIC_R;
            version = 0x00010301;
        }
        else {
            printf( "Invalid mi4 version: %s\n", &argv[1][4]);
            return -1;
        }

        iname = argv[2];
        oname = argv[3];
        
        if(!strncmp(argv[2], "-model=", 7)) {
            iname = argv[3];
            oname = argv[4];
            strncpy(model, &argv[2][7], 4);
            
            if(!strncmp(argv[3], "-type=", 6)) {
                iname = argv[4];
                oname = argv[5];
                strncpy(type, &argv[3][6], 4);
            }
        }

        return mi4_encode(iname, oname, version, mi4magic, model, type);
    }
    
    /* open file */
    file = fopen(iname,"rb");
    if (!file) {
       perror(iname);
       return -1;
    }
    fseek(file,0,SEEK_END);
    length = ftell(file);
    length = (length + 3) & ~3; /* Round up to nearest 4 byte boundary */
    

    fseek(file,0,SEEK_SET); 
    inbuf = malloc(length);
    if(method == add)
        outbuf = malloc(length + 8);
    else
        outbuf = malloc(length);
    if ( !inbuf || !outbuf ) {
       printf("out of memory!\n");
       return -1;
    }
    if(length> 4) {
        /* zero-fill the last 4 bytes to make sure there's no rubbish there
           when we write the size-aligned file later */
        memset(outbuf+length-4, 0, 4);
    }

    /* read file */
    i=fread(inbuf,1,length,file);
    if ( !i ) {
       perror(iname);
       return -1;
    }
    fclose(file);

    switch (method)
    {
        case add:
            for (i = 0; i < length; i++) {
                /* add 8 unsigned bits but keep a 32 bit sum */
                chksum += inbuf[i];
            }
            break;
        case none:
        default:
            /* dummy case just to silence picky compilers */
            break;
    }

    if(method == none) {
        /* calculate checksum */
        for (i=0;i<length;i++)
            crc += inbuf[i];
    }

    memset(header, 0, sizeof header);
    switch (method)
    {
        case add:
        {
            int2be(chksum, header); /* checksum, big-endian */
            memcpy(&header[4], modelname, 4); /* 4 bytes model name */
            memcpy(outbuf, inbuf, length); /* the input buffer to output*/
            headerlen = 8;
        }
        break;

        case tcc_sum:
            memcpy(outbuf, inbuf, length); /* the input buffer to output*/
            telechips_encode_sum(outbuf, length);
            break;

        case tcc_crc:
            memcpy(outbuf, inbuf, length); /* the input buffer to output*/
            telechips_encode_crc(outbuf, length);
            break;

#define MY_FIRMWARE_TYPE "Rockbox"
#define MY_HEADER_VERSION 1
        default:
            strncpy((char *)header, MY_FIRMWARE_TYPE,9);
            header[9]='\0'; /*shouldn't have to, but to be SURE */
            header[10]=MY_HEADER_VERSION&0xFF;
            header[11]=(crc>>8)&0xFF;
            header[12]=crc&0xFF;
            int2be(sizeof(header), &header[12]);
            break;
    }

    /* write file */
    file = fopen(oname,"wb");
    if ( !file ) {
       perror(oname);
       return -1;
    }
    if (headerlen > 0) {
       if ( !fwrite(header,headerlen,1,file) ) {
          perror(oname);
          return -1;
       }
    }
    if ( !fwrite(outbuf,length,1,file) ) {
       perror(oname);
       return -1;
    }
    fclose(file);
    
    free(inbuf);
    free(outbuf);
    
    return 0;
}

static int iaudio_encode(char *iname, char *oname, char *idstring)
{
    size_t len;
    int length;
    FILE *file;
    unsigned char *outbuf;
    int i;
    unsigned char sum = 0;
    
    file = fopen(iname, "rb");
    if (!file) {
       perror(iname);
       return -1;
    }
    fseek(file,0,SEEK_END);
    length = ftell(file);
    
    fseek(file,0,SEEK_SET); 
    outbuf = malloc(length+0x1030);

    if ( !outbuf ) {
       printf("out of memory!\n");
       return -1;
    }

    len = fread(outbuf+0x1030, 1, length, file);
    if(len < (size_t) length) {
        perror(iname);
        return -2;
    }
    
    memset(outbuf, 0, 0x1030);
    strcpy((char *)outbuf, idstring);
    memcpy(outbuf+0x20, iaudio_bl_flash, 
           BMPWIDTH_iaudio_bl_flash * (BMPHEIGHT_iaudio_bl_flash/8) * 2);
    short2be(BMPWIDTH_iaudio_bl_flash, &outbuf[0x10]);
    short2be((BMPHEIGHT_iaudio_bl_flash/8), &outbuf[0x12]);
    outbuf[0x19] = 2;

    for(i = 0; i < length;i++)
        sum += outbuf[0x1030 + i];

    int2be(length, &outbuf[0x1024]);
    outbuf[0x102b] = sum;

    fclose(file);

    file = fopen(oname, "wb");
    if (!file) {
       perror(oname);
       return -3;
    }
    
    len = fwrite(outbuf, 1, length+0x1030, file);
    if(len < (size_t)length) {
        perror(oname);
        return -4;
    }

    fclose(file);
    return 0;
}


/* Create an ipod firmware partition image 

   fw_ver = 2 for 3rd Gen ipods, 3 for all later ipods including 5g.

   This function doesn't yet handle the Broadcom resource image for the 5g,
   so the resulting images won't be usable.

   This has also only been tested on an ipod Photo 
*/

static int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc)
{
    static const char *apple_stop_sign = "{{~~  /-----\\   "\
                                         "{{~~ /       \\  "\
                                         "{{~~|         | "\
                                         "{{~~| S T O P | "\
                                         "{{~~|         | "\
                                         "{{~~ \\       /  "\
                                         "{{~~  \\-----/   "\
                                         "Copyright(C) 200"\
                                         "1 Apple Computer"\
                                         ", Inc.----------"\
                                         "----------------"\
                                         "----------------"\
                                         "----------------"\
                                         "----------------"\
                                         "----------------"\
                                         "---------------";
    size_t len;
    int length;
    int rsrclength;
    int rsrcoffset;
    FILE *file;
    unsigned int sum = 0;
    unsigned int rsrcsum = 0;
    unsigned char *outbuf;
    int bufsize;
    int i;

    file = fopen(iname, "rb");
    if (!file) {
       perror(iname);
       return -1;
    }
    fseek(file,0,SEEK_END);
    length = ftell(file);
    
    fseek(file,0,SEEK_SET);

    bufsize=(length+0x4600);
    if (fake_rsrc) {
        bufsize = (bufsize + 0x400) & ~0x200;
    }

    outbuf = malloc(bufsize);

    if ( !outbuf ) {
       printf("out of memory!\n");
       return -1;
    }

    len = fread(outbuf+0x4600, 1, length, file);
    if(len < (size_t)length) {
        perror(iname);
        return -2;
    }
    fclose(file);

    /* Calculate checksum for later use in header */    
    for(i = 0x4600; i < 0x4600+length;i++)
        sum += outbuf[i];

    /* Clear the header area to zero */
    memset(outbuf, 0, 0x4600);

    /* APPLE STOP SIGN */
    strcpy((char *)outbuf, apple_stop_sign);

    /* VOLUME HEADER */
    memcpy(&outbuf[0x100],"]ih[",4);   /* Magic */
    int2le(0x4000,   &outbuf[0x104]);  /* Firmware offset relative to 0x200 */
    short2le(0x10c,  &outbuf[0x108]);  /* Location of extended header */
    short2le(fw_ver, &outbuf[0x10a]);

    /* Firmware Directory - "osos" entry */
    memcpy(&outbuf[0x4200],"!ATAsoso",8);  /* dev and type */
    int2le(0,          &outbuf[0x4208]);   /* id */
    int2le(0x4400,     &outbuf[0x420c]);   /* devOffset */
    int2le(length,     &outbuf[0x4210]);   /* Length of firmware */
    int2le(0x10000000, &outbuf[0x4214]);   /* Addr */
    int2le(0,          &outbuf[0x4218]);   /* Entry Offset */
    int2le(sum,        &outbuf[0x421c]);   /* Checksum */
    int2le(0x00006012, &outbuf[0x4220]);   /* vers - 0x6012 is a guess */
    int2le(0xffffffff, &outbuf[0x4224]);   /* LoadAddr - for flash images */

    /* "rsrc" entry (if applicable) */
    if (fake_rsrc) {
        rsrcoffset=(length+0x4600+0x200) & ~0x200;
        rsrclength=0x200;
        rsrcsum=0;

        memcpy(&outbuf[0x4228],"!ATAcrsr",8); /* dev and type */
        int2le(0,          &outbuf[0x4230]);  /* id */
        int2le(rsrcoffset, &outbuf[0x4234]);  /* devOffset */
        int2le(rsrclength, &outbuf[0x4238]);  /* Length of firmware */
        int2le(0x10000000, &outbuf[0x423c]);  /* Addr */
        int2le(0,          &outbuf[0x4240]);  /* Entry Offset */
        int2le(rsrcsum,    &outbuf[0x4244]);  /* Checksum */
        int2le(0x0000b000, &outbuf[0x4248]);  /* vers */
        int2le(0xffffffff, &outbuf[0x424c]);  /* LoadAddr - for flash images */
    }

    file = fopen(oname, "wb");
    if (!file) {
       perror(oname);
       return -3;
    }
    
    len = fwrite(outbuf, 1, length+0x4600, file);
    if(len < (size_t)length) {
        perror(oname);
        return -4;
    }

    fclose(file);

    return 0;
}

#define CCPMP_SIZE 0x500000
static int ccpmp_encode(char *iname, char *oname)
{
    size_t len;
    int length;
    FILE *file;
    unsigned char *outbuf;

    file = fopen(iname, "rb");
    if (!file) {
       perror(iname);
       return -1;
    }
    fseek(file,0,SEEK_END);
    length = ftell(file);
    
    fseek(file,0,SEEK_SET);

    outbuf = malloc(CCPMP_SIZE);

    if ( !outbuf ) {
       printf("out of memory!\n");
       return -1;
    }

    len = fread(outbuf, 1, length, file);
    if(len < (size_t)length) {
        perror(iname);
        return -2;
    }
    fclose(file);

    /* Clear the tail area to 0xFF */
    memset(&outbuf[length], 0xFF, CCPMP_SIZE - length);

    /* Header */
    int2le(length, &outbuf[0x4]);

    file = fopen(oname, "wb");
    if (!file) {
       perror(oname);
       return -3;
    }
    
    len = fwrite(outbuf, 1, CCPMP_SIZE, file);
    if(len < (size_t)length) {
        perror(oname);
        return -4;
    }

    fclose(file);

    return 0;
}