diff options
Diffstat (limited to 'tools/iriver.c')
-rw-r--r-- | tools/iriver.c | 381 |
1 files changed, 381 insertions, 0 deletions
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 | |||
25 | const unsigned char munge[] = { | ||
26 | 0x7a, 0x36, 0xc4, 0x43, 0x49, 0x6b, 0x35, 0x4e, 0xa3, 0x46, 0x25, 0x84, | ||
27 | 0x4d, 0x73, 0x74, 0x61 | ||
28 | }; | ||
29 | |||
30 | const unsigned char header_modify[] = "* IHPFIRM-DECODED "; | ||
31 | |||
32 | const char * const models[] = { "iHP-100", "iHP-120/iHP-140", "H300 series", | ||
33 | NULL }; | ||
34 | |||
35 | /* aligns with models array; expected min firmware size */ | ||
36 | const unsigned int firmware_minsize[] = { 1000000, 1000000, 2000000 }; | ||
37 | /* aligns with models array; expected max firmware size */ | ||
38 | const unsigned int firmware_maxsize[] = { 2000000, 2000000, 4000000 }; | ||
39 | |||
40 | const 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 | |||
48 | static 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 | |||
63 | static 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 | |||
75 | static 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 | |||
87 | static 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 | |||
99 | int 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 | |||
261 | int 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 | }; | ||