summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2008-06-18 22:30:59 +0000
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2008-06-18 22:30:59 +0000
commitc47988034fbd5d7de8fcda2a87224bdb0b5dcfe6 (patch)
treedeb96279c6952871b5c0f47ec25879bb73240afd
parentefbd2b8d7a9295f1a5544717c1b86527cc27ac7b (diff)
downloadrockbox-c47988034fbd5d7de8fcda2a87224bdb0b5dcfe6.tar.gz
rockbox-c47988034fbd5d7de8fcda2a87224bdb0b5dcfe6.zip
Factor out scramble / mkboot functions to allow easier reuse (for rbutil).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17732 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--tools/descramble.c3
-rw-r--r--tools/iriver.c50
-rw-r--r--tools/iriver.h18
-rw-r--r--tools/mkboot.c43
-rw-r--r--tools/mkboot.h33
-rw-r--r--tools/scramble.c3
6 files changed, 116 insertions, 34 deletions
diff --git a/tools/descramble.c b/tools/descramble.c
index 3bf10a6b69..5d4975be26 100644
--- a/tools/descramble.c
+++ b/tools/descramble.c
@@ -87,8 +87,7 @@ int main (int argc, char** argv)
87 /* iRiver code dealt with in the iriver.c code */ 87 /* iRiver code dealt with in the iriver.c code */
88 iname = argv[2]; 88 iname = argv[2];
89 oname = argv[3]; 89 oname = argv[3];
90 iriver_decode(iname, oname, FALSE, STRIP_NONE); 90 return iriver_decode(iname, oname, FALSE, STRIP_NONE) ? -1 : 0;
91 return 0;
92 } 91 }
93 if(!strcmp(argv[1], "-gigabeat")) { 92 if(!strcmp(argv[1], "-gigabeat")) {
94 iname = argv[2]; 93 iname = argv[2];
diff --git a/tools/iriver.c b/tools/iriver.c
index 6730fbd8b2..4c949c6627 100644
--- a/tools/iriver.c
+++ b/tools/iriver.c
@@ -96,7 +96,7 @@ static FILE * openoutfile( const char * filename )
96 return F; 96 return F;
97} 97}
98 98
99int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, 99int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify,
100 enum striptype stripmode ) 100 enum striptype stripmode )
101{ 101{
102 FILE * infile = NULL; 102 FILE * infile = NULL;
@@ -120,7 +120,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
120 { 120 {
121 fprintf( stderr, "This doesn't look like a valid encrypted iHP " 121 fprintf( stderr, "This doesn't look like a valid encrypted iHP "
122 "firmware - reason: header length\n" ); 122 "firmware - reason: header length\n" );
123 exit( -1 ); 123 fclose(infile);
124 fclose(outfile);
125 return -1;
124 }; 126 };
125 127
126 i = testheader( headerdata ); 128 i = testheader( headerdata );
@@ -128,7 +130,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
128 { 130 {
129 fprintf( stderr, "This firmware is for an unknown model, or is not" 131 fprintf( stderr, "This firmware is for an unknown model, or is not"
130 " a valid encrypted iHP firmware\n" ); 132 " a valid encrypted iHP firmware\n" );
131 exit( -1 ); 133 fclose(infile);
134 fclose(outfile);
135 return -2;
132 }; 136 };
133 fprintf( stderr, "Model %s\n", models[ i ] ); 137 fprintf( stderr, "Model %s\n", models[ i ] );
134 138
@@ -149,7 +153,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
149 { 153 {
150 fprintf( stderr, "This doesn't look like a valid encrypted " 154 fprintf( stderr, "This doesn't look like a valid encrypted "
151 "iHP firmware - reason: file 'length' data\n" ); 155 "iHP firmware - reason: file 'length' data\n" );
152 exit( -1 ); 156 fclose(infile);
157 fclose(outfile);
158 return -3;
153 }; 159 };
154 160
155 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); 161 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) );
@@ -209,7 +215,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
209 { 215 {
210 fprintf( stderr, "This doesn't look like a valid encrypted " 216 fprintf( stderr, "This doesn't look like a valid encrypted "
211 "iHP firmware - reason: 'length2' mismatch\n" ); 217 "iHP firmware - reason: 'length2' mismatch\n" );
212 exit( -1 ); 218 fclose(infile);
219 fclose(outfile);
220 return -4;
213 }; 221 };
214 222
215 fp = 0; 223 fp = 0;
@@ -224,7 +232,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
224 { 232 {
225 fprintf( stderr, "This doesn't look like a valid encrypted " 233 fprintf( stderr, "This doesn't look like a valid encrypted "
226 "iHP firmware - reason: Checksum mismatch!" ); 234 "iHP firmware - reason: Checksum mismatch!" );
227 exit( -1 ); 235 fclose(infile);
236 fclose(outfile);
237 return -5;
228 }; 238 };
229 ppChecksums += lenread; 239 ppChecksums += lenread;
230 }; 240 };
@@ -233,7 +243,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
233 { 243 {
234 fprintf( stderr, "This doesn't look like a valid encrypted " 244 fprintf( stderr, "This doesn't look like a valid encrypted "
235 "iHP firmware - reason: 'length3' mismatch\n" ); 245 "iHP firmware - reason: 'length3' mismatch\n" );
236 exit( -1 ); 246 fclose(infile);
247 fclose(outfile);
248 return -6;
237 }; 249 };
238 250
239 251
@@ -258,7 +270,7 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify,
258 return 0; 270 return 0;
259} 271}
260 272
261int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) 273int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify )
262{ 274{
263 FILE * infile = NULL; 275 FILE * infile = NULL;
264 FILE * outfile = NULL; 276 FILE * outfile = NULL;
@@ -281,7 +293,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify )
281 { 293 {
282 fprintf( stderr, "This doesn't look like a valid decoded " 294 fprintf( stderr, "This doesn't look like a valid decoded "
283 "iHP firmware - reason: header length\n" ); 295 "iHP firmware - reason: header length\n" );
284 exit( -1 ); 296 fclose(infile);
297 fclose(outfile);
298 return -1;
285 }; 299 };
286 300
287 if( modify ) 301 if( modify )
@@ -294,7 +308,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify )
294 { 308 {
295 fprintf( stderr, "This firmware is for an unknown model, or is not" 309 fprintf( stderr, "This firmware is for an unknown model, or is not"
296 " a valid decoded iHP firmware\n" ); 310 " a valid decoded iHP firmware\n" );
297 exit( -1 ); 311 fclose(infile);
312 fclose(outfile);
313 return -2;
298 }; 314 };
299 fprintf( stderr, "Model %s\n", models[ i ] ); 315 fprintf( stderr, "Model %s\n", models[ i ] );
300 316
@@ -314,7 +330,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify )
314 { 330 {
315 fprintf( stderr, "This doesn't look like a valid decoded iHP" 331 fprintf( stderr, "This doesn't look like a valid decoded iHP"
316 " firmware - reason: file 'length' data\n" ); 332 " firmware - reason: file 'length' data\n" );
317 exit( -1 ); 333 fclose(infile);
334 fclose(outfile);
335 return -3;
318 }; 336 };
319 337
320 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); 338 pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) );
@@ -351,7 +369,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify )
351 { 369 {
352 fprintf( stderr, "This doesn't look like a valid decoded " 370 fprintf( stderr, "This doesn't look like a valid decoded "
353 "iHP firmware - reason: 'length1' mismatch\n" ); 371 "iHP firmware - reason: 'length1' mismatch\n" );
354 exit( -1 ); 372 fclose(infile);
373 fclose(outfile);
374 return -4;
355 }; 375 };
356 376
357 /* write out remainder w/out applying descrambler */ 377 /* write out remainder w/out applying descrambler */
@@ -370,10 +390,14 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify )
370 { 390 {
371 fprintf( stderr, "This doesn't look like a valid decoded " 391 fprintf( stderr, "This doesn't look like a valid decoded "
372 "iHP firmware - reason: 'length2' mismatch\n" ); 392 "iHP firmware - reason: 'length2' mismatch\n" );
373 exit( -1 ); 393 fclose(infile);
394 fclose(outfile);
395 return -5;
374 }; 396 };
375 397
376 fprintf( stderr, "File encoded successfully and checksum table built!\n" ); 398 fprintf( stderr, "File encoded successfully and checksum table built!\n" );
377 399
400 fclose(infile);
401 fclose(outfile);
378 return 0; 402 return 0;
379} 403}
diff --git a/tools/iriver.h b/tools/iriver.h
index 96326c4e48..e9df809a12 100644
--- a/tools/iriver.h
+++ b/tools/iriver.h
@@ -16,14 +16,21 @@
16 * KIND, either express or implied. 16 * KIND, either express or implied.
17 * 17 *
18 ****************************************************************************/ 18 ****************************************************************************/
19 19#ifndef TRUE
20#define TRUE 1 20#define TRUE 1
21#endif
22#ifndef FALSE
21#define FALSE 0 23#define FALSE 0
24#endif
22 25
23#define BOOL unsigned int 26#define BOOL unsigned int
24 27
25#define ESTF_SIZE 32 28#define ESTF_SIZE 32
26 29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
27enum striptype 34enum striptype
28{ 35{
29 STRIP_NONE, 36 STRIP_NONE,
@@ -32,6 +39,11 @@ enum striptype
32}; 39};
33 40
34/* protos for iriver.c */ 41/* protos for iriver.c */
35int iriver_decode(char *infile, char *outfile, BOOL modify, 42int iriver_decode(const char *infile, const char *outfile, BOOL modify,
36 enum striptype stripmode ); 43 enum striptype stripmode );
37int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ); 44int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify);
45
46#ifdef __cplusplus
47}
48#endif
49
diff --git a/tools/mkboot.c b/tools/mkboot.c
index ce743e9414..d86a4fc177 100644
--- a/tools/mkboot.c
+++ b/tools/mkboot.c
@@ -19,25 +19,25 @@
19#include <stdio.h> 19#include <stdio.h>
20#include <stdlib.h> 20#include <stdlib.h>
21#include <string.h> 21#include <string.h>
22#include "mkboot.h"
22 23
23void usage(void) 24#ifndef RBUTIL
25static void usage(void)
24{ 26{
25 printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\n"); 27 printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\n");
26 28
27 exit(1); 29 exit(1);
28} 30}
31#endif
29 32
30unsigned char image[0x400000 + 0x220 + 0x400000/0x200]; 33static unsigned char image[0x400000 + 0x220 + 0x400000/0x200];
31 34
35#ifndef RBUTIL
32int main(int argc, char *argv[]) 36int main(int argc, char *argv[])
33{ 37{
34 char *infile, *bootfile, *outfile; 38 char *infile, *bootfile, *outfile;
35 FILE *f;
36 int i;
37 int len;
38 int actual_length, total_length, binary_length, num_chksums;
39 int origin = 0x1f0000; /* H1x0 bootloader address */ 39 int origin = 0x1f0000; /* H1x0 bootloader address */
40 40
41 if(argc < 3) { 41 if(argc < 3) {
42 usage(); 42 usage();
43 } 43 }
@@ -55,6 +55,16 @@ int main(int argc, char *argv[])
55 bootfile = argv[2]; 55 bootfile = argv[2];
56 outfile = argv[3]; 56 outfile = argv[3];
57 } 57 }
58 return mkboot(infile, bootfile, outfile, origin);
59}
60#endif
61
62int mkboot(const char* infile, const char* bootfile, const char* outfile, int origin)
63{
64 FILE *f;
65 int i;
66 int len;
67 int actual_length, total_length, binary_length, num_chksums;
58 68
59 memset(image, 0xff, sizeof(image)); 69 memset(image, 0xff, sizeof(image));
60 70
@@ -62,13 +72,14 @@ int main(int argc, char *argv[])
62 f = fopen(infile, "rb"); 72 f = fopen(infile, "rb");
63 if(!f) { 73 if(!f) {
64 perror(infile); 74 perror(infile);
65 exit(1); 75 return -1;
66 } 76 }
67 77
68 i = fread(image, 1, 16, f); 78 i = fread(image, 1, 16, f);
69 if(i < 16) { 79 if(i < 16) {
70 perror(infile); 80 perror(infile);
71 exit(1); 81 fclose(f);
82 return -2;
72 } 83 }
73 84
74 /* This is the length of the binary image without the scrambling 85 /* This is the length of the binary image without the scrambling
@@ -81,7 +92,8 @@ int main(int argc, char *argv[])
81 i = fread(image+16, 1, len, f); 92 i = fread(image+16, 1, len, f);
82 if(i < len) { 93 if(i < len) {
83 perror(infile); 94 perror(infile);
84 exit(1); 95 fclose(f);
96 return -3;
85 } 97 }
86 98
87 fclose(f); 99 fclose(f);
@@ -90,7 +102,8 @@ int main(int argc, char *argv[])
90 f = fopen(bootfile, "rb"); 102 f = fopen(bootfile, "rb");
91 if(!f) { 103 if(!f) {
92 perror(bootfile); 104 perror(bootfile);
93 exit(1); 105 fclose(f);
106 return -4;
94 } 107 }
95 108
96 fseek(f, 0, SEEK_END); 109 fseek(f, 0, SEEK_END);
@@ -101,7 +114,8 @@ int main(int argc, char *argv[])
101 i = fread(image+0x220 + origin, 1, len, f); 114 i = fread(image+0x220 + origin, 1, len, f);
102 if(i < len) { 115 if(i < len) {
103 perror(bootfile); 116 perror(bootfile);
104 exit(1); 117 fclose(f);
118 return -5;
105 } 119 }
106 120
107 fclose(f); 121 fclose(f);
@@ -109,7 +123,7 @@ int main(int argc, char *argv[])
109 f = fopen(outfile, "wb"); 123 f = fopen(outfile, "wb");
110 if(!f) { 124 if(!f) {
111 perror(outfile); 125 perror(outfile);
112 exit(1); 126 return -6;
113 } 127 }
114 128
115 /* Patch the reset vector to start the boot loader */ 129 /* Patch the reset vector to start the boot loader */
@@ -161,7 +175,8 @@ int main(int argc, char *argv[])
161 i = fwrite(image, 1, total_length, f); 175 i = fwrite(image, 1, total_length, f);
162 if(i < total_length) { 176 if(i < total_length) {
163 perror(outfile); 177 perror(outfile);
164 exit(1); 178 fclose(f);
179 return -7;
165 } 180 }
166 181
167 printf("Wrote 0x%x bytes in %s\n", total_length, outfile); 182 printf("Wrote 0x%x bytes in %s\n", total_length, outfile);
diff --git a/tools/mkboot.h b/tools/mkboot.h
new file mode 100644
index 0000000000..ea9862157d
--- /dev/null
+++ b/tools/mkboot.h
@@ -0,0 +1,33 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 by Dominik Riebeling
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 MKBOOT_H
21#define MKBOOT_H
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27int mkboot(const char* infile, const char* bootfile, const char* outfile, int origin);
28
29#ifdef __cplusplus
30}
31#endif
32#endif
33
diff --git a/tools/scramble.c b/tools/scramble.c
index c02c78916b..5263809e41 100644
--- a/tools/scramble.c
+++ b/tools/scramble.c
@@ -287,8 +287,7 @@ int main (int argc, char** argv)
287 /* iRiver code dealt with in the iriver.c code */ 287 /* iRiver code dealt with in the iriver.c code */
288 iname = argv[2]; 288 iname = argv[2];
289 oname = argv[3]; 289 oname = argv[3];
290 iriver_encode(iname, oname, FALSE); 290 return (iriver_encode(iname, oname, FALSE) != 0) ? -1 : 0;
291 return 0;
292 } 291 }
293 else if(!strcmp(argv[1], "-gigabeat")) { 292 else if(!strcmp(argv[1], "-gigabeat")) {
294 /* iRiver code dealt with in the iriver.c code */ 293 /* iRiver code dealt with in the iriver.c code */