diff options
-rw-r--r-- | docs/CREDITS | 1 | ||||
-rw-r--r-- | tools/Makefile | 5 | ||||
-rw-r--r-- | tools/scramble.c | 39 | ||||
-rw-r--r-- | tools/telechips.c | 156 | ||||
-rw-r--r-- | tools/telechips.h | 26 |
5 files changed, 220 insertions, 7 deletions
diff --git a/docs/CREDITS b/docs/CREDITS index 83967930d8..0f09162eaf 100644 --- a/docs/CREDITS +++ b/docs/CREDITS | |||
@@ -336,6 +336,7 @@ Alexander Eickhoff | |||
336 | Pinitnun Shanasabang | 336 | Pinitnun Shanasabang |
337 | Ken Fazzone | 337 | Ken Fazzone |
338 | David Bishop | 338 | David Bishop |
339 | Hein-Pieter van Braam | ||
339 | 340 | ||
340 | The libmad team | 341 | The libmad team |
341 | The wavpack team | 342 | The wavpack team |
diff --git a/tools/Makefile b/tools/Makefile index 74d82affab..bbb9b0d9b0 100644 --- a/tools/Makefile +++ b/tools/Makefile | |||
@@ -16,15 +16,16 @@ CLEANALL := scramble descramble iriver sh2d bmp2rb rdf2binary convbdf \ | |||
16 | all: | 16 | all: |
17 | @echo "Run make in your build directory!" | 17 | @echo "Run make in your build directory!" |
18 | 18 | ||
19 | scramble: scramble.o iriver.o mi4.o gigabeat.o gigabeats.o | 19 | scramble: scramble.o iriver.o mi4.o gigabeat.o gigabeats.o telechips.o |
20 | descramble: descramble.o iriver.o gigabeat.o | 20 | descramble: descramble.o iriver.o gigabeat.o |
21 | 21 | ||
22 | scramble.o: scramble.c iriver.h mi4.h gigabeat.h | 22 | scramble.o: scramble.c iriver.h mi4.h gigabeat.h telechips.h |
23 | descramble.o: descramble.c iriver.h gigabeat.h | 23 | descramble.o: descramble.c iriver.h gigabeat.h |
24 | iriver.o: iriver.c iriver.h | 24 | iriver.o: iriver.c iriver.h |
25 | gigabeat.o: gigabeat.c gigabeat.h | 25 | gigabeat.o: gigabeat.c gigabeat.h |
26 | gigabeats.o: gigabeats.c gigabeats.h | 26 | gigabeats.o: gigabeats.c gigabeats.h |
27 | mi4.o: mi4.c mi4.h | 27 | mi4.o: mi4.c mi4.h |
28 | telechips.o: telechips.c telechips.h | ||
28 | 29 | ||
29 | sh2d: sh2d.c | 30 | sh2d: sh2d.c |
30 | 31 | ||
diff --git a/tools/scramble.c b/tools/scramble.c index 8534d41832..ad128835c5 100644 --- a/tools/scramble.c +++ b/tools/scramble.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include "gigabeat.h" | 25 | #include "gigabeat.h" |
26 | #include "gigabeats.h" | 26 | #include "gigabeats.h" |
27 | #include "mi4.h" | 27 | #include "mi4.h" |
28 | #include "telechips.h" | ||
28 | 29 | ||
29 | int iaudio_encode(char *iname, char *oname, char *idstring); | 30 | int iaudio_encode(char *iname, char *oname, char *idstring); |
30 | int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc); | 31 | int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc); |
@@ -102,6 +103,7 @@ void usage(void) | |||
102 | "\t -model=XXXX where XXXX is the model id string\n" | 103 | "\t -model=XXXX where XXXX is the model id string\n" |
103 | "\t -type=XXXX where XXXX is a string indicating the \n" | 104 | "\t -type=XXXX where XXXX is a string indicating the \n" |
104 | "\t type of binary, eg. RBOS, RBBL\n" | 105 | "\t type of binary, eg. RBOS, RBBL\n" |
106 | "\t-tcc=X Telechips generic firmware format (X values: sum, crc)\n" | ||
105 | "\t-add=X Rockbox generic \"add-up\" checksum format\n" | 107 | "\t-add=X Rockbox generic \"add-up\" checksum format\n" |
106 | "\t (X values: h100, h120, h140, h300, ipco, nano, ipvd, mn2g\n" | 108 | "\t (X values: h100, h120, h140, h300, ipco, nano, ipvd, mn2g\n" |
107 | "\t ip3g, ip4g, mini, iax5, h10, h10_5gb, tpj2,\n" | 109 | "\t ip3g, ip4g, mini, iax5, h10, h10_5gb, tpj2,\n" |
@@ -127,7 +129,7 @@ int main (int argc, char** argv) | |||
127 | unsigned long modelnum; | 129 | unsigned long modelnum; |
128 | char modelname[5]; | 130 | char modelname[5]; |
129 | int model_id; | 131 | int model_id; |
130 | enum { none, scramble, xor, add } method = scramble; | 132 | enum { none, scramble, xor, tcc_sum, tcc_crc, add } method = scramble; |
131 | 133 | ||
132 | model_id = ARCHOS_PLAYER; | 134 | model_id = ARCHOS_PLAYER; |
133 | 135 | ||
@@ -186,6 +188,20 @@ int main (int argc, char** argv) | |||
186 | return -1; | 188 | return -1; |
187 | } | 189 | } |
188 | } | 190 | } |
191 | else if(!strncmp(argv[1], "-tcc=", 4)) { | ||
192 | headerlen = 0; | ||
193 | iname = argv[2]; | ||
194 | oname = argv[3]; | ||
195 | |||
196 | if(!strcmp(&argv[1][5], "sum")) | ||
197 | method = tcc_sum; | ||
198 | else if(!strcmp(&argv[1][5], "crc")) | ||
199 | method = tcc_crc; | ||
200 | else { | ||
201 | fprintf(stderr, "unsupported TCC method: %s\n", &argv[1][5]); | ||
202 | return 2; | ||
203 | } | ||
204 | } | ||
189 | else if(!strncmp(argv[1], "-add=", 5)) { | 205 | else if(!strncmp(argv[1], "-add=", 5)) { |
190 | iname = argv[2]; | 206 | iname = argv[2]; |
191 | oname = argv[3]; | 207 | oname = argv[3]; |
@@ -409,7 +425,7 @@ int main (int argc, char** argv) | |||
409 | break; | 425 | break; |
410 | } | 426 | } |
411 | 427 | ||
412 | if(method != add) { | 428 | if((method == none) || (method == scramble) || (method == xor)) { |
413 | /* calculate checksum */ | 429 | /* calculate checksum */ |
414 | for (i=0;i<length;i++) | 430 | for (i=0;i<length;i++) |
415 | crc += inbuf[i]; | 431 | crc += inbuf[i]; |
@@ -426,6 +442,17 @@ int main (int argc, char** argv) | |||
426 | headerlen = 8; | 442 | headerlen = 8; |
427 | } | 443 | } |
428 | break; | 444 | break; |
445 | |||
446 | case tcc_sum: | ||
447 | memcpy(outbuf, inbuf, length); /* the input buffer to output*/ | ||
448 | telechips_encode_sum(outbuf, length); | ||
449 | break; | ||
450 | |||
451 | case tcc_crc: | ||
452 | memcpy(outbuf, inbuf, length); /* the input buffer to output*/ | ||
453 | telechips_encode_crc(outbuf, length); | ||
454 | break; | ||
455 | |||
429 | case scramble: | 456 | case scramble: |
430 | if (headerlen == 6) { | 457 | if (headerlen == 6) { |
431 | int2be(length, header); | 458 | int2be(length, header); |
@@ -488,9 +515,11 @@ int main (int argc, char** argv) | |||
488 | perror(oname); | 515 | perror(oname); |
489 | return -1; | 516 | return -1; |
490 | } | 517 | } |
491 | if ( !fwrite(header,headerlen,1,file) ) { | 518 | if (headerlen > 0) { |
492 | perror(oname); | 519 | if ( !fwrite(header,headerlen,1,file) ) { |
493 | return -1; | 520 | perror(oname); |
521 | return -1; | ||
522 | } | ||
494 | } | 523 | } |
495 | if ( !fwrite(outbuf,length,1,file) ) { | 524 | if ( !fwrite(outbuf,length,1,file) ) { |
496 | perror(oname); | 525 | perror(oname); |
diff --git a/tools/telechips.c b/tools/telechips.c new file mode 100644 index 0000000000..5b6f3c23b3 --- /dev/null +++ b/tools/telechips.c | |||
@@ -0,0 +1,156 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Telechips firmware checksum support for scramble | ||
11 | * | ||
12 | * Copyright (C) 2007 Dave Chapman | ||
13 | * | ||
14 | * Thanks to Hein-Pieter van Braam for his work in identifying the | ||
15 | * CRC algorithm used. | ||
16 | * | ||
17 | * All files in this archive are subject to the GNU General Public License. | ||
18 | * See the file COPYING in the source tree root for full license agreement. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | #include <unistd.h> | ||
27 | #include <sys/types.h> | ||
28 | #include <sys/stat.h> | ||
29 | #include <fcntl.h> | ||
30 | #include <stdint.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | static uint32_t crctable[256]; | ||
34 | |||
35 | /* Simple implementation of a function to reverse the bottom n bits in x */ | ||
36 | static uint32_t bitreverse(uint32_t x,int n) | ||
37 | { | ||
38 | int i; | ||
39 | uint32_t mask = 1<<(n-1); | ||
40 | uint32_t res = 0; | ||
41 | |||
42 | for (i=0; i<n; i++) | ||
43 | { | ||
44 | if (x & 1) | ||
45 | res |= mask; | ||
46 | |||
47 | x >>= 1; | ||
48 | mask >>= 1; | ||
49 | } | ||
50 | return res; | ||
51 | } | ||
52 | |||
53 | /* Generate a lookup table for a reverse CRC32 */ | ||
54 | static void gentable(uint32_t poly) | ||
55 | { | ||
56 | int i; | ||
57 | uint32_t r; | ||
58 | uint32_t index; | ||
59 | |||
60 | for (index = 0; index < 256; index++) | ||
61 | { | ||
62 | r = bitreverse(index,8) << 24; | ||
63 | for (i=0; i<8; i++) | ||
64 | { | ||
65 | if (r & (1 << 31)) | ||
66 | r = (r << 1) ^ poly; | ||
67 | else | ||
68 | r<<=1; | ||
69 | } | ||
70 | crctable[index] = bitreverse(r,32); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | /* Perform a reverse CRC32 */ | ||
75 | static uint32_t calc_crc(unsigned char *message, int size) | ||
76 | { | ||
77 | uint32_t crc = 0; | ||
78 | int i; | ||
79 | |||
80 | for (i=0; i < size; i++){ | ||
81 | if ((i < 0x10) || (i >= 0x18)) { | ||
82 | crc = crctable[((crc ^ (message[i])) & 0xff)] ^ (crc >> 8); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return crc; | ||
87 | } | ||
88 | |||
89 | /* Endian-safe functions to read/write a 32-bit little-endian integer */ | ||
90 | |||
91 | static uint32_t get_uint32le(unsigned char* p) | ||
92 | { | ||
93 | return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); | ||
94 | } | ||
95 | |||
96 | static void put_uint32le(unsigned char* p, uint32_t x) | ||
97 | { | ||
98 | p[0] = x & 0xff; | ||
99 | p[1] = (x >> 8) & 0xff; | ||
100 | p[2] = (x >> 16) & 0xff; | ||
101 | p[3] = (x >> 24) & 0xff; | ||
102 | } | ||
103 | |||
104 | /* A simple checksum - seems to be used by the TCC76x firmwares */ | ||
105 | void telechips_encode_sum(unsigned char* buf, int length) | ||
106 | { | ||
107 | uint32_t sum; | ||
108 | int i; | ||
109 | |||
110 | /* Set checksum field to 0 */ | ||
111 | put_uint32le(buf + 0x10, 0); | ||
112 | |||
113 | /* Perform a simple sum, treating the file as a series of 32-bit | ||
114 | little-endian integers */ | ||
115 | sum = 0; | ||
116 | for (i=0; i < length; i+=4) { | ||
117 | sum += get_uint32le(buf + i); | ||
118 | } | ||
119 | /* Negate the sum - this means that the sum of the whole file | ||
120 | (including this value) will be equal to zero */ | ||
121 | sum = -sum; | ||
122 | |||
123 | /* Set the checksum field */ | ||
124 | put_uint32le(buf + 0x10, sum); | ||
125 | } | ||
126 | |||
127 | |||
128 | /* Two reverse CRC32 checksums - seems to be used by the TCC77x firmwares */ | ||
129 | void telechips_encode_crc(unsigned char* buf, int length) | ||
130 | { | ||
131 | uint32_t crc1,crc2; | ||
132 | |||
133 | /* Generate the CRC table */ | ||
134 | gentable(0x8001801BL); | ||
135 | |||
136 | /* Clear the existing CRC values */ | ||
137 | put_uint32le(buf+0x10, 0); | ||
138 | put_uint32le(buf+0x18, 0); | ||
139 | |||
140 | /* Write the length */ | ||
141 | put_uint32le(buf+0x1c, length); | ||
142 | |||
143 | /* Calculate the first CRC - over the entire file */ | ||
144 | crc1 = calc_crc(buf, length); | ||
145 | |||
146 | /* What happens next depends on the filesize */ | ||
147 | if (length >= 128*1024) | ||
148 | { | ||
149 | put_uint32le(buf+0x18, crc1); | ||
150 | |||
151 | crc2 = calc_crc(buf, 128*1024); | ||
152 | put_uint32le(buf+0x10, crc2); | ||
153 | } else { | ||
154 | put_uint32le(buf+0x10, crc1); | ||
155 | } | ||
156 | } | ||
diff --git a/tools/telechips.h b/tools/telechips.h new file mode 100644 index 0000000000..7854da0294 --- /dev/null +++ b/tools/telechips.h | |||
@@ -0,0 +1,26 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 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 | #ifndef _TELECHIPS_H | ||
21 | #define _TELECHIPS_H | ||
22 | |||
23 | void telechips_encode_sum(unsigned char* buf, int length); | ||
24 | void telechips_encode_crc(unsigned char* buf, int length); | ||
25 | |||
26 | #endif | ||