summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Chapman <dave@dchapman.com>2007-09-30 12:49:43 +0000
committerDave Chapman <dave@dchapman.com>2007-09-30 12:49:43 +0000
commit41541c5c31c5cd4f1a6dbbd7354046cfd5cfa13f (patch)
treea57742b5bb57f6b9d5cd5700601fcf9e22ebebf7
parent0f5d9f9125e53ae62175469e78151ca4cb0be79a (diff)
downloadrockbox-41541c5c31c5cd4f1a6dbbd7354046cfd5cfa13f.tar.gz
rockbox-41541c5c31c5cd4f1a6dbbd7354046cfd5cfa13f.zip
Add support for the generic Telechips firmware format checksums - use -tcc=sum if the header just contains a single checksum, or -tcc=crc if the header contains two 32-bit CRCs. Credit goes to Hein-Pieter van Braam for his work on identifying the (reverse) CRC32 algorithm used when calculating the two CRCs
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14917 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--docs/CREDITS1
-rw-r--r--tools/Makefile5
-rw-r--r--tools/scramble.c39
-rw-r--r--tools/telechips.c156
-rw-r--r--tools/telechips.h26
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
336Pinitnun Shanasabang 336Pinitnun Shanasabang
337Ken Fazzone 337Ken Fazzone
338David Bishop 338David Bishop
339Hein-Pieter van Braam
339 340
340The libmad team 341The libmad team
341The wavpack team 342The 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 \
16all: 16all:
17 @echo "Run make in your build directory!" 17 @echo "Run make in your build directory!"
18 18
19scramble: scramble.o iriver.o mi4.o gigabeat.o gigabeats.o 19scramble: scramble.o iriver.o mi4.o gigabeat.o gigabeats.o telechips.o
20descramble: descramble.o iriver.o gigabeat.o 20descramble: descramble.o iriver.o gigabeat.o
21 21
22scramble.o: scramble.c iriver.h mi4.h gigabeat.h 22scramble.o: scramble.c iriver.h mi4.h gigabeat.h telechips.h
23descramble.o: descramble.c iriver.h gigabeat.h 23descramble.o: descramble.c iriver.h gigabeat.h
24iriver.o: iriver.c iriver.h 24iriver.o: iriver.c iriver.h
25gigabeat.o: gigabeat.c gigabeat.h 25gigabeat.o: gigabeat.c gigabeat.h
26gigabeats.o: gigabeats.c gigabeats.h 26gigabeats.o: gigabeats.c gigabeats.h
27mi4.o: mi4.c mi4.h 27mi4.o: mi4.c mi4.h
28telechips.o: telechips.c telechips.h
28 29
29sh2d: sh2d.c 30sh2d: 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
29int iaudio_encode(char *iname, char *oname, char *idstring); 30int iaudio_encode(char *iname, char *oname, char *idstring);
30int ipod_encode(char *iname, char *oname, int fw_ver, bool fake_rsrc); 31int 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
33static uint32_t crctable[256];
34
35/* Simple implementation of a function to reverse the bottom n bits in x */
36static 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 */
54static 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 */
75static 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
91static uint32_t get_uint32le(unsigned char* p)
92{
93 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
94}
95
96static 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 */
105void 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 */
129void 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
23void telechips_encode_sum(unsigned char* buf, int length);
24void telechips_encode_crc(unsigned char* buf, int length);
25
26#endif