summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile7
-rwxr-xr-xtools/configure2
-rw-r--r--tools/descramble.c26
-rw-r--r--tools/iriver.c381
-rw-r--r--tools/iriver.h37
-rw-r--r--tools/scramble.c37
6 files changed, 474 insertions, 16 deletions
diff --git a/tools/Makefile b/tools/Makefile
index 43218d99f6..8e82d0f8ee 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -13,9 +13,12 @@ TARGETS := scramble descramble sh2d bmp2rb convbdf generate_rocklatin
13all: $(TARGETS) 13all: $(TARGETS)
14 @echo "tools done" 14 @echo "tools done"
15 15
16scramble: scramble.c 16scramble: scramble.o iriver.o
17descramble: descramble.o iriver.o
17 18
18descramble: descramble.c 19scramble.o: scramble.c iriver.h
20descramble.o: descramble.c iriver.h
21iriver.o: iriver.c iriver.h
19 22
20sh2d: sh2d.c 23sh2d: sh2d.c
21 24
diff --git a/tools/configure b/tools/configure
index 4834fa3e30..1c58a39e4a 100755
--- a/tools/configure
+++ b/tools/configure
@@ -352,7 +352,7 @@ if [ -z "$archos" ]; then
352 target="-DIRIVER_H100" 352 target="-DIRIVER_H100"
353 memory=32 # always 353 memory=32 # always
354 coldfirecc 354 coldfirecc
355 tool="cp" 355 tool="$rootdir/tools/scramble -iriver"
356 output="rockbox.iriver" 356 output="rockbox.iriver"
357 appextra="recorder" 357 appextra="recorder"
358 archosrom="" 358 archosrom=""
diff --git a/tools/descramble.c b/tools/descramble.c
index 7ed32139c6..7376190c69 100644
--- a/tools/descramble.c
+++ b/tools/descramble.c
@@ -20,6 +20,20 @@
20#include <stdio.h> 20#include <stdio.h>
21#include <stdlib.h> 21#include <stdlib.h>
22 22
23#include "iriver.h"
24
25void usage(void)
26{
27 printf("usage: descramble [options] <input file> <output file>\n");
28 printf("options:\n"
29 "\t-fm Archos FM recorder format\n"
30 "\t-v2 Archos V2 recorder format\n"
31 "\t-mm=X Archos Multimedia format (X values: A=JBMM, B=AV1xx, C=AV3xx)\n"
32 "\t-iriver iRiver format\n"
33 "\nNo option assumes Archos standard player/recorder format.\n");
34 exit(1);
35}
36
23int main (int argc, char** argv) 37int main (int argc, char** argv)
24{ 38{
25 unsigned long length,i,slen; 39 unsigned long length,i,slen;
@@ -32,9 +46,7 @@ int main (int argc, char** argv)
32 FILE* file; 46 FILE* file;
33 47
34 if (argc < 3) { 48 if (argc < 3) {
35 printf("usage: %s [-fm] [-v2] [-mm] <input file> <output file>\n", 49 usage();
36 argv[0]);
37 return -1;
38 } 50 }
39 51
40 if (!strcmp(argv[1], "-fm") || !strcmp(argv[1], "-v2")) { 52 if (!strcmp(argv[1], "-fm") || !strcmp(argv[1], "-v2")) {
@@ -49,6 +61,14 @@ int main (int argc, char** argv)
49 oname = argv[3]; 61 oname = argv[3];
50 descramble = 0; 62 descramble = 0;
51 } 63 }
64
65 if(!strcmp(argv[1], "-iriver")) {
66 /* iRiver code dealt with in the iriver.c code */
67 iname = argv[2];
68 oname = argv[3];
69 iriver_decode(iname, oname, FALSE, STRIP_NONE);
70 return 0;
71 }
52 72
53 /* open file and check size */ 73 /* open file and check size */
54 file = fopen(iname,"rb"); 74 file = fopen(iname,"rb");
diff --git a/tools/iriver.c b/tools/iriver.c
new file mode 100644
index 0000000000..0beab80cb2
--- /dev/null
+++ b/tools/iriver.c
@@ -0,0 +1,381 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2004 by Dave Hooper
11 *
12 * This particular source code file is licensed under the X11 license. See the
13 * bottom of the COPYING file for details on this license.
14 *
15 * Original code from http://www.beermex.com/@spc/ihpfirm.src.zip
16 * Details at http://www.rockbox.org/twiki/bin/view/Main/IriverToolsGuide
17 *
18 ****************************************************************************/
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "iriver.h"
24
25const unsigned char munge[] = {
26 0x7a, 0x36, 0xc4, 0x43, 0x49, 0x6b, 0x35, 0x4e, 0xa3, 0x46, 0x25, 0x84,
27 0x4d, 0x73, 0x74, 0x61
28};
29
30const unsigned char header_modify[] = "* IHPFIRM-DECODED ";
31
32const char * const models[] = { "iHP-100", "iHP-120/iHP-140", "H300 series",
33 NULL };
34
35/* aligns with models array; expected min firmware size */
36const unsigned int firmware_minsize[] = { 1000000, 1000000, 2000000 };
37/* aligns with models array; expected max firmware size */
38const unsigned int firmware_maxsize[] = { 2000000, 2000000, 4000000 };
39
40const unsigned char header[][16] = {
41 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
42 { 0x20, 0x03, 0x08, 0x27, 0x24, 0x00, 0x02, 0x30, 0x19, 0x17, 0x65, 0x73,
43 0x85, 0x32, 0x83, 0x22 },
44 { 0x20, 0x04, 0x03, 0x27, 0x20, 0x50, 0x01, 0x70, 0x80, 0x30, 0x80, 0x06,
45 0x30, 0x19, 0x17, 0x65 }
46};
47
48static int testheader( const unsigned char * const data )
49{
50 const unsigned char * const d = data+16;
51 const char * const * m = models;
52 int index = 0;
53 while( *m )
54 {
55 if( memcmp( header[ index ], d, 16 ) == 0 )
56 return index;
57 index++;
58 m++;
59 };
60 return -1;
61};
62
63static void modifyheader( unsigned char * data )
64{
65 const unsigned char * h = header_modify;
66 int i;
67 for( i=0; i<512; i++ )
68 {
69 if( *h == '\0' )
70 h = header_modify;
71 *data++ ^= *h++;
72 };
73};
74
75static FILE * openinfile( const char * filename )
76{
77 FILE * F = fopen( filename, "rb" );
78 if( F == NULL )
79 {
80 fprintf( stderr, "Couldn't open input file %s\n", filename );
81 perror( "Error was " );
82 exit( -1 );
83 };
84 return F;
85};
86
87static FILE * openoutfile( const char * filename )
88{
89 FILE * F = fopen( filename, "wb" );
90 if( F == NULL )
91 {
92 fprintf( stderr, "Couldn't open output file %s\n", filename );
93 perror( "Error was " );
94 exit( -1 );
95 };
96 return F;
97};
98
99int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
100 enum striptype stripmode )
101{
102 FILE * infile = NULL;
103 FILE * outfile = NULL;
104 int i = -1;
105 unsigned char headerdata[512];
106 unsigned long dwLength1, dwLength2, dwLength3, fp = 0;
107 unsigned char blockdata[16+16];
108 unsigned char out[16];
109 unsigned char newmunge;
110 signed long lenread;
111 int s = 0;
112 unsigned char * pChecksums, * ppChecksums = 0;
113 unsigned char ck;
114
115 infile = openinfile(infile_name);
116 outfile = openoutfile(outfile_name);
117
118 lenread = fread( headerdata, 1, 512, infile );
119 if( lenread != 512 )
120 {
121 fprintf( stderr, "This doesn't look like a valid encrypted iHP "
122 "firmware - reason: header length\n" );
123 exit( -1 );
124 };
125
126 i = testheader( headerdata );
127 if( i == -1 )
128 {
129 fprintf( stderr, "This firmware is for an unknown model, or is not"
130 " a valid encrypted iHP firmware\n" );
131 exit( -1 );
132 };
133 fprintf( stderr, "Model %s\n", models[ i ] );
134
135 dwLength1 = headerdata[0] | (headerdata[1]<<8) |
136 (headerdata[2]<<16) | (headerdata[3]<<24);
137 dwLength2 = headerdata[4] | (headerdata[5]<<8) |
138 (headerdata[6]<<16) | (headerdata[7]<<24);
139 dwLength3 = headerdata[8] | (headerdata[9]<<8) |
140 (headerdata[10]<<16) | (headerdata[11]<<24);
141
142 if( dwLength1 < firmware_minsize[ i ] ||
143 dwLength1 > firmware_maxsize[ i ] ||
144 dwLength2 < firmware_minsize[ i ] ||
145 dwLength2 > dwLength1 ||
146 dwLength3 > dwLength1 ||
147 dwLength2>>9 != dwLength3 ||
148 dwLength2+dwLength3+512 != dwLength1 )
149 {
150 fprintf( stderr, "This doesn't look like a valid encrypted "
151 "iHP firmware - reason: file 'length' data\n" );
152 exit( -1 );
153 };
154
155 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) );
156
157 if( modify )
158 {
159 modifyheader( headerdata );
160 };
161
162 if( stripmode == STRIP_NONE )
163 fwrite( headerdata, 512, 1, outfile );
164
165 memset( blockdata, 0, 16 );
166
167 ck = 0;
168 while( ( fp < dwLength2 ) &&
169 ( lenread = fread( blockdata+16, 1, 16, infile ) ) == 16 )
170 {
171 fp += 16;
172
173 for( i=0; i<16; ++i )
174 {
175 newmunge = blockdata[16+i] ^ munge[i];
176 out[i] = newmunge ^ blockdata[i];
177 blockdata[i] = newmunge;
178 ck += out[i];
179 }
180
181 if( fp > ESTF_SIZE || stripmode != STRIP_HEADER_CHECKSUM_ESTF )
182 {
183 fwrite( out+4, 1, 12, outfile );
184 fwrite( out, 1, 4, outfile );
185 }
186 else
187 {
188 if( ESTF_SIZE - fp < 16 )
189 {
190 memcpy( out+4, blockdata+16, 12 );
191 memcpy( out, blockdata+28, 4 );
192 fwrite( blockdata+16+ESTF_SIZE-fp, 1, ESTF_SIZE-fp, outfile );
193 }
194 }
195
196
197 if( s == 496 )
198 {
199 s = 0;
200 memset( blockdata, 0, 16 );
201 *ppChecksums++ = ck;
202 ck = 0;
203 }
204 else
205 s+=16;
206 };
207
208 if( fp != dwLength2 )
209 {
210 fprintf( stderr, "This doesn't look like a valid encrypted "
211 "iHP firmware - reason: 'length2' mismatch\n" );
212 exit( -1 );
213 };
214
215 fp = 0;
216 ppChecksums = pChecksums;
217 while( ( fp < dwLength3 ) &&
218 ( lenread = fread( blockdata, 1, 32, infile ) ) > 0 )
219 {
220 fp += lenread;
221 if( stripmode == STRIP_NONE )
222 fwrite( blockdata, 1, lenread, outfile );
223 if( memcmp( ppChecksums, blockdata, lenread ) != 0 )
224 {
225 fprintf( stderr, "This doesn't look like a valid encrypted "
226 "iHP firmware - reason: Checksum mismatch!" );
227 exit( -1 );
228 };
229 ppChecksums += lenread;
230 };
231
232 if( fp != dwLength3 )
233 {
234 fprintf( stderr, "This doesn't look like a valid encrypted "
235 "iHP firmware - reason: 'length3' mismatch\n" );
236 exit( -1 );
237 };
238
239
240 fprintf( stderr, "File decoded correctly and all checksums matched!\n" );
241 switch( stripmode )
242 {
243 default:
244 case STRIP_NONE:
245 fprintf(stderr, "Output file contains all headers and "
246 "checksums\n");
247 break;
248 case STRIP_HEADER_CHECKSUM:
249 fprintf( stderr, "NB: output file contains only ESTFBINR header"
250 " and decoded firmware code\n" );
251 break;
252 case STRIP_HEADER_CHECKSUM_ESTF:
253 fprintf( stderr, "NB: output file contains only raw decoded "
254 "firmware code\n" );
255 break;
256 };
257
258 return 0;
259};
260
261int iriver_encode(char *infile_name, char *outfile_name, BOOL modify )
262{
263 FILE * infile = NULL;
264 FILE * outfile = NULL;
265 int i = -1;
266 unsigned char headerdata[512];
267 unsigned long dwLength1, dwLength2, dwLength3, fp = 0;
268 unsigned char blockdata[16+16];
269 unsigned char out[16];
270 unsigned char newmunge;
271 signed long lenread;
272 int s = 0;
273 unsigned char * pChecksums, * ppChecksums;
274 unsigned char ck;
275
276 enum striptype stripmode = STRIP_NONE;
277
278 infile = openinfile(infile_name);
279 outfile = openoutfile(outfile_name);
280
281 lenread = fread( headerdata, 1, 512, infile );
282 if( lenread != 512 )
283 {
284 fprintf( stderr, "This doesn't look like a valid decoded "
285 "iHP firmware - reason: header length\n" );
286 exit( -1 );
287 };
288
289 if( modify )
290 {
291 modifyheader( headerdata ); /* reversible */
292 };
293
294 i = testheader( headerdata );
295 if( i == -1 )
296 {
297 fprintf( stderr, "This firmware is for an unknown model, or is not"
298 " a valid decoded iHP firmware\n" );
299 exit( -1 );
300 };
301 fprintf( stderr, "Model %s\n", models[ i ] );
302
303 dwLength1 = headerdata[0] | (headerdata[1]<<8) |
304 (headerdata[2]<<16) | (headerdata[3]<<24);
305 dwLength2 = headerdata[4] | (headerdata[5]<<8) |
306 (headerdata[6]<<16) | (headerdata[7]<<24);
307 dwLength3 = headerdata[8] | (headerdata[9]<<8) |
308 (headerdata[10]<<16) | (headerdata[11]<<24);
309
310 if( dwLength1 < firmware_minsize[i] ||
311 dwLength1 > firmware_maxsize[i] ||
312 dwLength2 < firmware_minsize[i] ||
313 dwLength2 > dwLength1 ||
314 dwLength3 > dwLength1 ||
315 dwLength2+dwLength3+512 != dwLength1 )
316 {
317 fprintf( stderr, "This doesn't look like a valid decoded iHP"
318 " firmware - reason: file 'length' data\n" );
319 exit( -1 );
320 };
321
322 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) );
323
324 fwrite( headerdata, 512, 1, outfile );
325
326 memset( blockdata, 0, 16 );
327 ck = 0;
328 while( ( fp < dwLength2 ) &&
329 ( lenread = fread( blockdata+16, 1, 16, infile ) ) == 16 )
330 {
331 fp += 16;
332 for( i=0; i<16; ++i )
333 {
334 newmunge = blockdata[16+((12+i)&0xf)] ^ blockdata[i];
335 out[i] = newmunge ^ munge[i];
336 ck += blockdata[16+i];
337 blockdata[i] = newmunge;
338 };
339 fwrite( out, 1, 16, outfile );
340
341 if( s == 496 )
342 {
343 s = 0;
344 memset( blockdata, 0, 16 );
345 *ppChecksums++ = ck;
346 ck = 0;
347 }
348 else
349 s+=16;
350 };
351
352 if( fp != dwLength2 )
353 {
354 fprintf( stderr, "This doesn't look like a valid decoded "
355 "iHP firmware - reason: 'length1' mismatch\n" );
356 exit( -1 );
357 };
358
359 /* write out remainder w/out applying descrambler */
360 fp = 0;
361 lenread = dwLength3;
362 ppChecksums = pChecksums;
363 while( ( fp < dwLength3) &&
364 ( lenread = fwrite( ppChecksums, 1, lenread, outfile ) ) > 0 )
365 {
366 fp += lenread;
367 ppChecksums += lenread;
368 lenread = dwLength3 - fp;
369 };
370
371 if( fp != dwLength3 )
372 {
373 fprintf( stderr, "This doesn't look like a valid decoded "
374 "iHP firmware - reason: 'length2' mismatch\n" );
375 exit( -1 );
376 };
377
378 fprintf( stderr, "File encoded successfully and checksum table built!\n" );
379
380 return 0;
381};
diff --git a/tools/iriver.h b/tools/iriver.h
new file mode 100644
index 0000000000..96326c4e48
--- /dev/null
+++ b/tools/iriver.h
@@ -0,0 +1,37 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Daniel Stenberg
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#define TRUE 1
21#define FALSE 0
22
23#define BOOL unsigned int
24
25#define ESTF_SIZE 32
26
27enum striptype
28{
29 STRIP_NONE,
30 STRIP_HEADER_CHECKSUM,
31 STRIP_HEADER_CHECKSUM_ESTF
32};
33
34/* protos for iriver.c */
35int iriver_decode(char *infile, char *outfile, BOOL modify,
36 enum striptype stripmode );
37int iriver_encode(char *infile_name, char *outfile_name, BOOL modify );
diff --git a/tools/scramble.c b/tools/scramble.c
index 4e3addfc26..df6c5ac084 100644
--- a/tools/scramble.c
+++ b/tools/scramble.c
@@ -20,6 +20,8 @@
20#include <stdio.h> 20#include <stdio.h>
21#include <stdlib.h> 21#include <stdlib.h>
22 22
23#include "iriver.h"
24
23void int2le(unsigned int val, unsigned char* addr) 25void int2le(unsigned int val, unsigned char* addr)
24{ 26{
25 addr[0] = val & 0xFF; 27 addr[0] = val & 0xFF;
@@ -36,6 +38,22 @@ void int2be(unsigned int val, unsigned char* addr)
36 addr[3] = val & 0xFF; 38 addr[3] = val & 0xFF;
37} 39}
38 40
41void usage(void)
42{
43 printf("usage: scramble [options] <input file> <output file> [xor string]\n");
44 printf("options:\n"
45 "\t-fm Archos FM recorder format\n"
46 "\t-v2 Archos V2 recorder format\n"
47 "\t-ofm Archos Ondio FM recorder format\n"
48 "\t-osp Archos Ondio SP format\n"
49 "\t-neo SSI Neo format\n"
50 "\t-mm=X Archos Multimedia format (X values: A=JBMM, B=AV1xx, C=AV3xx)\n"
51 "\t-iriver iRiver format\n"
52 "\nNo option results in Archos standard player/recorder format.\n");
53
54 exit(1);
55}
56
39int main (int argc, char** argv) 57int main (int argc, char** argv)
40{ 58{
41 unsigned long length,i,slen; 59 unsigned long length,i,slen;
@@ -51,16 +69,7 @@ int main (int argc, char** argv)
51 enum { none, scramble, xor } method = scramble; 69 enum { none, scramble, xor } method = scramble;
52 70
53 if (argc < 3) { 71 if (argc < 3) {
54 printf("usage: %s [options] <input file> <output file> [xor string]\n",argv[0]); 72 usage();
55 printf("options:\n");
56 printf("\t-fm Archos FM recorder format\n");
57 printf("\t-v2 Archos V2 recorder format\n");
58 printf("\t-ofm Archos Ondio FM recorder format\n");
59 printf("\t-osp Archos Ondio SP format\n");
60 printf("\t-neo SSI Neo format\n");
61 printf("\t-mm=X Archos Multimedia format (X values: A=JBMM, B=AV1xx, C=AV3xx)\n");
62 printf("\nNo option results in Archos standard player/recorder format.\n");
63 return -1;
64 } 73 }
65 74
66 if(!strcmp(argv[1], "-fm")) { 75 if(!strcmp(argv[1], "-fm")) {
@@ -111,6 +120,14 @@ int main (int argc, char** argv)
111 } 120 }
112 } 121 }
113 122
123 else if(!strcmp(argv[1], "-iriver")) {
124 /* iRiver code dealt with in the iriver.c code */
125 iname = argv[2];
126 oname = argv[3];
127 iriver_encode(iname, oname, FALSE);
128 return 0;
129 }
130
114 /* open file */ 131 /* open file */
115 file = fopen(iname,"rb"); 132 file = fopen(iname,"rb");
116 if (!file) { 133 if (!file) {