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
This commit is contained in:
Dominik Riebeling 2008-06-18 22:30:59 +00:00
parent efbd2b8d7a
commit c47988034f
6 changed files with 116 additions and 34 deletions

View File

@ -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];

View File

@ -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;
}

View File

@ -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

View File

@ -19,25 +19,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mkboot.h"
void usage(void)
#ifndef RBUTIL
static void usage(void)
{
printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\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);

33
tools/mkboot.h Normal file
View File

@ -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

View File

@ -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 */