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) /* iRiver code dealt with in the iriver.c code */ iname = argv[2]; oname = argv[3]; - iriver_decode(iname, oname, FALSE, STRIP_NONE); - return 0; + return iriver_decode(iname, oname, FALSE, STRIP_NONE) ? -1 : 0; } if(!strcmp(argv[1], "-gigabeat")) { 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 ) return F; } -int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, +int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify, enum striptype stripmode ) { FILE * infile = NULL; @@ -120,7 +120,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, { fprintf( stderr, "This doesn't look like a valid encrypted iHP " "firmware - reason: header length\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -1; }; i = testheader( headerdata ); @@ -128,7 +130,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, { fprintf( stderr, "This firmware is for an unknown model, or is not" " a valid encrypted iHP firmware\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -2; }; fprintf( stderr, "Model %s\n", models[ i ] ); @@ -149,7 +153,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: file 'length' data\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -3; }; pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); @@ -209,7 +215,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: 'length2' mismatch\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -4; }; fp = 0; @@ -224,7 +232,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: Checksum mismatch!" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -5; }; ppChecksums += lenread; }; @@ -233,7 +243,9 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: 'length3' mismatch\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -6; }; @@ -258,7 +270,7 @@ int iriver_decode(char *infile_name, char *outfile_name, BOOL modify, return 0; } -int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) +int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify ) { FILE * infile = NULL; FILE * outfile = NULL; @@ -281,7 +293,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) { fprintf( stderr, "This doesn't look like a valid decoded " "iHP firmware - reason: header length\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -1; }; if( modify ) @@ -294,7 +308,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) { fprintf( stderr, "This firmware is for an unknown model, or is not" " a valid decoded iHP firmware\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -2; }; fprintf( stderr, "Model %s\n", models[ i ] ); @@ -314,7 +330,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) { fprintf( stderr, "This doesn't look like a valid decoded iHP" " firmware - reason: file 'length' data\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -3; }; pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); @@ -351,7 +369,9 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) { fprintf( stderr, "This doesn't look like a valid decoded " "iHP firmware - reason: 'length1' mismatch\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -4; }; /* write out remainder w/out applying descrambler */ @@ -370,10 +390,14 @@ int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ) { fprintf( stderr, "This doesn't look like a valid decoded " "iHP firmware - reason: 'length2' mismatch\n" ); - exit( -1 ); + fclose(infile); + fclose(outfile); + return -5; }; fprintf( stderr, "File encoded successfully and checksum table built!\n" ); + fclose(infile); + fclose(outfile); return 0; } 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 @@ * KIND, either express or implied. * ****************************************************************************/ - +#ifndef TRUE #define TRUE 1 +#endif +#ifndef FALSE #define FALSE 0 +#endif #define BOOL unsigned int #define ESTF_SIZE 32 +#ifdef __cplusplus +extern "C" { +#endif + enum striptype { STRIP_NONE, @@ -32,6 +39,11 @@ enum striptype }; /* protos for iriver.c */ -int iriver_decode(char *infile, char *outfile, BOOL modify, +int iriver_decode(const char *infile, const char *outfile, BOOL modify, enum striptype stripmode ); -int iriver_encode(char *infile_name, char *outfile_name, BOOL modify ); +int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify); + +#ifdef __cplusplus +} +#endif + 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 @@ #include #include #include +#include "mkboot.h" -void usage(void) +#ifndef RBUTIL +static void usage(void) { printf("usage: mkboot [-h300] \n"); exit(1); } +#endif -unsigned char image[0x400000 + 0x220 + 0x400000/0x200]; +static unsigned char image[0x400000 + 0x220 + 0x400000/0x200]; +#ifndef RBUTIL int main(int argc, char *argv[]) { char *infile, *bootfile, *outfile; - FILE *f; - int i; - int len; - int actual_length, total_length, binary_length, num_chksums; int origin = 0x1f0000; /* H1x0 bootloader address */ - + if(argc < 3) { usage(); } @@ -55,6 +55,16 @@ int main(int argc, char *argv[]) bootfile = argv[2]; outfile = argv[3]; } + return mkboot(infile, bootfile, outfile, origin); +} +#endif + +int mkboot(const char* infile, const char* bootfile, const char* outfile, int origin) +{ + FILE *f; + int i; + int len; + int actual_length, total_length, binary_length, num_chksums; memset(image, 0xff, sizeof(image)); @@ -62,13 +72,14 @@ int main(int argc, char *argv[]) f = fopen(infile, "rb"); if(!f) { perror(infile); - exit(1); + return -1; } i = fread(image, 1, 16, f); if(i < 16) { perror(infile); - exit(1); + fclose(f); + return -2; } /* This is the length of the binary image without the scrambling @@ -81,7 +92,8 @@ int main(int argc, char *argv[]) i = fread(image+16, 1, len, f); if(i < len) { perror(infile); - exit(1); + fclose(f); + return -3; } fclose(f); @@ -90,7 +102,8 @@ int main(int argc, char *argv[]) f = fopen(bootfile, "rb"); if(!f) { perror(bootfile); - exit(1); + fclose(f); + return -4; } fseek(f, 0, SEEK_END); @@ -101,7 +114,8 @@ int main(int argc, char *argv[]) i = fread(image+0x220 + origin, 1, len, f); if(i < len) { perror(bootfile); - exit(1); + fclose(f); + return -5; } fclose(f); @@ -109,7 +123,7 @@ int main(int argc, char *argv[]) f = fopen(outfile, "wb"); if(!f) { perror(outfile); - exit(1); + return -6; } /* Patch the reset vector to start the boot loader */ @@ -161,7 +175,8 @@ int main(int argc, char *argv[]) i = fwrite(image, 1, total_length, f); if(i < total_length) { perror(outfile); - exit(1); + fclose(f); + return -7; } 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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2008 by Dominik Riebeling + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#ifndef MKBOOT_H +#define MKBOOT_H + +#ifdef __cplusplus +extern "C" { +#endif + +int mkboot(const char* infile, const char* bootfile, const char* outfile, int origin); + +#ifdef __cplusplus +} +#endif +#endif + 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) /* iRiver code dealt with in the iriver.c code */ iname = argv[2]; oname = argv[3]; - iriver_encode(iname, oname, FALSE); - return 0; + return (iriver_encode(iname, oname, FALSE) != 0) ? -1 : 0; } else if(!strcmp(argv[1], "-gigabeat")) { /* iRiver code dealt with in the iriver.c code */