2002-03-28 14:34:13 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2005-11-14 18:58:30 +00:00
|
|
|
|
#include <string.h>
|
2005-01-24 09:01:48 +00:00
|
|
|
|
#include "iriver.h"
|
|
|
|
|
|
2005-12-27 23:14:49 +00:00
|
|
|
|
int iaudio_encode(char *iname, char *oname);
|
|
|
|
|
|
2005-09-14 07:48:45 +00:00
|
|
|
|
enum
|
|
|
|
|
{
|
2005-09-14 11:15:59 +00:00
|
|
|
|
ARCHOS_PLAYER, /* and V1 recorder */
|
2005-09-14 07:48:45 +00:00
|
|
|
|
ARCHOS_V2RECORDER,
|
|
|
|
|
ARCHOS_FMRECORDER,
|
|
|
|
|
ARCHOS_ONDIO_SP,
|
|
|
|
|
ARCHOS_ONDIO_FM
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int size_limit[] =
|
|
|
|
|
{
|
|
|
|
|
0x32000, /* ARCHOS_PLAYER */
|
|
|
|
|
0x64000, /* ARCHOS_V2RECORDER */
|
|
|
|
|
0x64000, /* ARCHOS_FMRECORDER */
|
|
|
|
|
0x64000, /* ARCHOS_ONDIO_SP */
|
|
|
|
|
0x64000 /* ARCHOS_ONDIO_FM */
|
|
|
|
|
};
|
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
|
void int2le(unsigned int val, unsigned char* addr)
|
|
|
|
|
{
|
|
|
|
|
addr[0] = val & 0xFF;
|
|
|
|
|
addr[1] = (val >> 8) & 0xff;
|
|
|
|
|
addr[2] = (val >> 16) & 0xff;
|
|
|
|
|
addr[3] = (val >> 24) & 0xff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void int2be(unsigned int val, unsigned char* addr)
|
|
|
|
|
{
|
|
|
|
|
addr[0] = (val >> 24) & 0xff;
|
|
|
|
|
addr[1] = (val >> 16) & 0xff;
|
|
|
|
|
addr[2] = (val >> 8) & 0xff;
|
|
|
|
|
addr[3] = val & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-24 09:01:48 +00:00
|
|
|
|
void usage(void)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: scramble [options] <input file> <output file> [xor string]\n");
|
|
|
|
|
printf("options:\n"
|
|
|
|
|
"\t-fm Archos FM recorder format\n"
|
|
|
|
|
"\t-v2 Archos V2 recorder format\n"
|
|
|
|
|
"\t-ofm Archos Ondio FM recorder format\n"
|
|
|
|
|
"\t-osp Archos Ondio SP format\n"
|
|
|
|
|
"\t-neo SSI Neo format\n"
|
|
|
|
|
"\t-mm=X Archos Multimedia format (X values: A=JBMM, B=AV1xx, C=AV3xx)\n"
|
|
|
|
|
"\t-iriver iRiver format\n"
|
2005-12-27 23:14:49 +00:00
|
|
|
|
"\t-iaudio iAudio format\n"
|
2005-11-07 22:20:55 +00:00
|
|
|
|
"\t-add=X Rockbox generic \"add-up\" checksum format\n"
|
2006-02-05 18:30:50 +00:00
|
|
|
|
"\t (X values: h100, h120, h140, h300, ipco, nano, ipvd\n"
|
|
|
|
|
"\t ip3g, ip4g)\n"
|
2005-01-24 09:01:48 +00:00
|
|
|
|
"\nNo option results in Archos standard player/recorder format.\n");
|
|
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
|
int main (int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
unsigned long length,i,slen;
|
|
|
|
|
unsigned char *inbuf,*outbuf;
|
|
|
|
|
unsigned short crc=0;
|
2005-01-28 10:28:34 +00:00
|
|
|
|
unsigned long chksum=0; /* 32 bit checksum */
|
2002-12-19 15:00:40 +00:00
|
|
|
|
unsigned char header[24];
|
2005-11-14 18:58:30 +00:00
|
|
|
|
char *iname = argv[1];
|
|
|
|
|
char *oname = argv[2];
|
|
|
|
|
char *xorstring;
|
2002-12-19 15:00:40 +00:00
|
|
|
|
int headerlen = 6;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
FILE* file;
|
2003-10-27 18:04:28 +00:00
|
|
|
|
int version;
|
2005-11-07 22:20:55 +00:00
|
|
|
|
unsigned long modelnum;
|
|
|
|
|
char modelname[5];
|
2005-09-14 07:48:45 +00:00
|
|
|
|
int model_id;
|
2005-01-24 13:14:14 +00:00
|
|
|
|
enum { none, scramble, xor, add } method = scramble;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
2005-09-14 07:48:45 +00:00
|
|
|
|
model_id = ARCHOS_PLAYER;
|
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
|
if (argc < 3) {
|
2005-01-24 09:01:48 +00:00
|
|
|
|
usage();
|
2002-03-28 14:34:13 +00:00
|
|
|
|
}
|
2002-12-19 15:00:40 +00:00
|
|
|
|
|
2003-10-27 18:04:28 +00:00
|
|
|
|
if(!strcmp(argv[1], "-fm")) {
|
2002-12-19 15:00:40 +00:00
|
|
|
|
headerlen = 24;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
2003-10-27 18:04:28 +00:00
|
|
|
|
version = 4;
|
2005-09-14 07:48:45 +00:00
|
|
|
|
model_id = ARCHOS_FMRECORDER;
|
2003-10-27 18:04:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
else if(!strcmp(argv[1], "-v2")) {
|
2003-10-27 18:04:28 +00:00
|
|
|
|
headerlen = 24;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
version = 2;
|
2005-09-14 07:48:45 +00:00
|
|
|
|
model_id = ARCHOS_V2RECORDER;
|
2002-12-19 15:00:40 +00:00
|
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
|
|
2004-09-08 21:21:22 +00:00
|
|
|
|
else if(!strcmp(argv[1], "-ofm")) {
|
|
|
|
|
headerlen = 24;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
version = 8;
|
2005-09-14 07:48:45 +00:00
|
|
|
|
model_id = ARCHOS_ONDIO_FM;
|
2004-09-08 21:21:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if(!strcmp(argv[1], "-osp")) {
|
|
|
|
|
headerlen = 24;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
version = 16;
|
2005-09-14 07:48:45 +00:00
|
|
|
|
model_id = ARCHOS_ONDIO_SP;
|
2004-09-08 21:21:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
else if(!strcmp(argv[1], "-neo")) {
|
|
|
|
|
headerlen = 17;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
2004-01-08 21:34:52 +00:00
|
|
|
|
method = none;
|
|
|
|
|
}
|
|
|
|
|
else if(!strncmp(argv[1], "-mm=", 4)) {
|
|
|
|
|
headerlen = 16;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
method = xor;
|
|
|
|
|
version = argv[1][4];
|
|
|
|
|
if (argc > 4)
|
|
|
|
|
xorstring = argv[4];
|
|
|
|
|
else {
|
|
|
|
|
printf("Multimedia needs an xor string\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
|
}
|
2005-01-24 13:14:14 +00:00
|
|
|
|
else if(!strncmp(argv[1], "-add=", 5)) {
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
method = add;
|
|
|
|
|
|
|
|
|
|
if(!strcmp(&argv[1][5], "h120"))
|
2005-11-07 22:20:55 +00:00
|
|
|
|
modelnum = 0;
|
2005-01-24 13:14:14 +00:00
|
|
|
|
else if(!strcmp(&argv[1][5], "h140"))
|
2005-11-07 22:20:55 +00:00
|
|
|
|
modelnum = 0; /* the same as the h120 */
|
2005-01-24 13:14:14 +00:00
|
|
|
|
else if(!strcmp(&argv[1][5], "h100"))
|
2005-11-07 22:20:55 +00:00
|
|
|
|
modelnum = 1;
|
2005-01-24 13:14:14 +00:00
|
|
|
|
else if(!strcmp(&argv[1][5], "h300"))
|
2005-11-07 22:20:55 +00:00
|
|
|
|
modelnum = 2;
|
|
|
|
|
else if(!strcmp(&argv[1][5], "ipco"))
|
|
|
|
|
modelnum = 3;
|
|
|
|
|
else if(!strcmp(&argv[1][5], "nano"))
|
|
|
|
|
modelnum = 4;
|
2005-12-18 13:04:00 +00:00
|
|
|
|
else if(!strcmp(&argv[1][5], "ipvd"))
|
|
|
|
|
modelnum = 5;
|
2006-01-12 00:35:50 +00:00
|
|
|
|
else if(!strcmp(&argv[1][5], "fp7x"))
|
|
|
|
|
modelnum = 6;
|
2006-02-05 18:30:50 +00:00
|
|
|
|
else if(!strcmp(&argv[1][5], "ip3g"))
|
|
|
|
|
modelnum = 7;
|
|
|
|
|
else if(!strcmp(&argv[1][5], "ip4g"))
|
|
|
|
|
modelnum = 8;
|
2005-01-24 13:14:14 +00:00
|
|
|
|
else {
|
|
|
|
|
fprintf(stderr, "unsupported model: %s\n", &argv[1][5]);
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
/* we store a 4-letter model name too, for humans */
|
2005-11-07 22:20:55 +00:00
|
|
|
|
strcpy(modelname, &argv[1][5]);
|
|
|
|
|
chksum = modelnum; /* start checksum calcs with this */
|
2005-01-24 13:14:14 +00:00
|
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
|
|
2005-01-24 09:01:48 +00:00
|
|
|
|
else if(!strcmp(argv[1], "-iriver")) {
|
|
|
|
|
/* iRiver code dealt with in the iriver.c code */
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
iriver_encode(iname, oname, FALSE);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-12-27 23:14:49 +00:00
|
|
|
|
else if(!strcmp(argv[1], "-iaudio")) {
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
return iaudio_encode(iname, oname);
|
|
|
|
|
}
|
2005-01-24 09:01:48 +00:00
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
|
/* open file */
|
2002-12-19 15:00:40 +00:00
|
|
|
|
file = fopen(iname,"rb");
|
2002-03-28 14:34:13 +00:00
|
|
|
|
if (!file) {
|
2002-12-19 15:00:40 +00:00
|
|
|
|
perror(iname);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
fseek(file,0,SEEK_END);
|
|
|
|
|
length = ftell(file);
|
2003-04-12 21:17:47 +00:00
|
|
|
|
length = (length + 3) & ~3; /* Round up to nearest 4 byte boundary */
|
|
|
|
|
|
2005-09-14 07:48:45 +00:00
|
|
|
|
if ((method == scramble) &&
|
|
|
|
|
((length + headerlen) >= size_limit[model_id])) {
|
2005-12-06 13:44:01 +00:00
|
|
|
|
printf("error: firmware image is %d bytes while max size is %d!\n",
|
|
|
|
|
length + headerlen,
|
|
|
|
|
size_limit[model_id]);
|
2003-04-14 09:59:47 +00:00
|
|
|
|
fclose(file);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2005-09-14 07:48:45 +00:00
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
|
inbuf = malloc(length);
|
2004-01-08 21:34:52 +00:00
|
|
|
|
if (method == xor)
|
|
|
|
|
outbuf = malloc(length*2);
|
2005-01-24 13:14:14 +00:00
|
|
|
|
else if(method == add)
|
|
|
|
|
outbuf = malloc(length + 8);
|
2004-01-08 21:34:52 +00:00
|
|
|
|
else
|
|
|
|
|
outbuf = malloc(length);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
if ( !inbuf || !outbuf ) {
|
|
|
|
|
printf("out of memory!\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2005-01-24 13:14:14 +00:00
|
|
|
|
if(length> 4) {
|
|
|
|
|
/* zero-fill the last 4 bytes to make sure there's no rubbish there
|
|
|
|
|
when we write the size-aligned file later */
|
|
|
|
|
memset(outbuf+length-4, 0, 4);
|
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
|
|
|
|
/* read file */
|
|
|
|
|
i=fread(inbuf,1,length,file);
|
|
|
|
|
if ( !i ) {
|
2002-12-19 15:00:40 +00:00
|
|
|
|
perror(iname);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
|
switch (method)
|
|
|
|
|
{
|
2005-01-24 13:14:14 +00:00
|
|
|
|
case add:
|
2005-01-28 12:20:20 +00:00
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
/* add 8 unsigned bits but keep a 32 bit sum */
|
|
|
|
|
chksum += inbuf[i];
|
2005-01-24 13:14:14 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2004-01-08 21:34:52 +00:00
|
|
|
|
case scramble:
|
|
|
|
|
slen = length/4;
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
unsigned long addr = (i >> 2) + ((i % 4) * slen);
|
|
|
|
|
unsigned char data = inbuf[i];
|
|
|
|
|
data = ~((data << 1) | ((data >> 7) & 1)); /* poor man's ROL */
|
|
|
|
|
outbuf[addr] = data;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case xor:
|
|
|
|
|
/* "compress" */
|
|
|
|
|
slen = 0;
|
|
|
|
|
for (i=0; i<length; i++) {
|
|
|
|
|
if (!(i&7))
|
|
|
|
|
outbuf[slen++] = 0xff; /* all data is uncompressed */
|
|
|
|
|
outbuf[slen++] = inbuf[i];
|
|
|
|
|
}
|
|
|
|
|
break;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
}
|
2005-01-24 13:14:14 +00:00
|
|
|
|
|
|
|
|
|
if(method != add) {
|
|
|
|
|
/* calculate checksum */
|
|
|
|
|
for (i=0;i<length;i++)
|
|
|
|
|
crc += inbuf[i];
|
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
2002-12-19 15:00:40 +00:00
|
|
|
|
memset(header, 0, sizeof header);
|
2004-01-08 21:34:52 +00:00
|
|
|
|
switch (method)
|
|
|
|
|
{
|
2005-01-24 13:14:14 +00:00
|
|
|
|
case add:
|
|
|
|
|
{
|
2005-01-28 10:28:34 +00:00
|
|
|
|
int2be(chksum, header); /* checksum, big-endian */
|
2005-11-07 22:20:55 +00:00
|
|
|
|
memcpy(&header[4], modelname, 4); /* 4 bytes model name */
|
2005-01-24 13:14:14 +00:00
|
|
|
|
memcpy(outbuf, inbuf, length); /* the input buffer to output*/
|
|
|
|
|
headerlen = 8;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2004-01-08 21:34:52 +00:00
|
|
|
|
case scramble:
|
|
|
|
|
if (headerlen == 6) {
|
|
|
|
|
int2be(length, header);
|
|
|
|
|
header[4] = (crc >> 8) & 0xff;
|
|
|
|
|
header[5] = crc & 0xff;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
header[0] =
|
|
|
|
|
header[1] =
|
|
|
|
|
header[2] =
|
|
|
|
|
header[3] = 0xff; /* ??? */
|
|
|
|
|
|
|
|
|
|
header[6] = (crc >> 8) & 0xff;
|
|
|
|
|
header[7] = crc & 0xff;
|
|
|
|
|
|
|
|
|
|
header[11] = version;
|
|
|
|
|
|
|
|
|
|
header[15] = headerlen; /* really? */
|
|
|
|
|
|
|
|
|
|
int2be(length, &header[20]);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2003-12-12 14:07:28 +00:00
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
|
case xor:
|
|
|
|
|
{
|
|
|
|
|
int xorlen = strlen(xorstring);
|
2002-12-19 15:00:40 +00:00
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
|
/* xor data */
|
|
|
|
|
for (i=0; i<slen; i++)
|
|
|
|
|
outbuf[i] ^= xorstring[i & (xorlen-1)];
|
|
|
|
|
|
2004-03-17 23:43:42 +00:00
|
|
|
|
/* calculate checksum */
|
|
|
|
|
for (i=0; i<slen; i++)
|
|
|
|
|
crc += outbuf[i];
|
|
|
|
|
|
2004-01-08 21:34:52 +00:00
|
|
|
|
header[0] = header[2] = 'Z';
|
|
|
|
|
header[1] = header[3] = version;
|
|
|
|
|
int2le(length, &header[4]);
|
|
|
|
|
int2le(slen, &header[8]);
|
|
|
|
|
int2le(crc, &header[12]);
|
|
|
|
|
length = slen;
|
|
|
|
|
break;
|
2003-12-12 14:07:28 +00:00
|
|
|
|
}
|
2004-01-08 21:34:52 +00:00
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
#define MY_FIRMWARE_TYPE "Rockbox"
|
|
|
|
|
#define MY_HEADER_VERSION 1
|
2004-01-08 21:34:52 +00:00
|
|
|
|
default:
|
2005-11-14 18:58:30 +00:00
|
|
|
|
strncpy((char *)header, MY_FIRMWARE_TYPE,9);
|
2004-01-08 21:34:52 +00:00
|
|
|
|
header[9]='\0'; /*shouldn't have to, but to be SURE */
|
|
|
|
|
header[10]=MY_HEADER_VERSION&0xFF;
|
|
|
|
|
header[11]=(crc>>8)&0xFF;
|
|
|
|
|
header[12]=crc&0xFF;
|
|
|
|
|
int2be(sizeof(header), &header[12]);
|
|
|
|
|
break;
|
2002-12-19 15:00:40 +00:00
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
|
|
|
|
/* write file */
|
2002-12-19 15:00:40 +00:00
|
|
|
|
file = fopen(oname,"wb");
|
2002-03-28 14:34:13 +00:00
|
|
|
|
if ( !file ) {
|
2002-12-19 15:00:40 +00:00
|
|
|
|
perror(oname);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2002-12-19 15:00:40 +00:00
|
|
|
|
if ( !fwrite(header,headerlen,1,file) ) {
|
|
|
|
|
perror(oname);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if ( !fwrite(outbuf,length,1,file) ) {
|
2002-12-19 15:00:40 +00:00
|
|
|
|
perror(oname);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
|
|
free(inbuf);
|
|
|
|
|
free(outbuf);
|
|
|
|
|
|
2003-04-14 09:59:47 +00:00
|
|
|
|
return 0;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
}
|
2005-12-27 23:14:49 +00:00
|
|
|
|
|
|
|
|
|
int iaudio_encode(char *iname, char *oname)
|
|
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
int length;
|
|
|
|
|
FILE *file;
|
|
|
|
|
unsigned char *outbuf;
|
|
|
|
|
int i;
|
|
|
|
|
unsigned char sum = 0;
|
|
|
|
|
|
|
|
|
|
file = fopen(iname, "rb");
|
|
|
|
|
if (!file) {
|
|
|
|
|
perror(iname);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
fseek(file,0,SEEK_END);
|
|
|
|
|
length = ftell(file);
|
|
|
|
|
|
|
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
|
outbuf = malloc(length+0x1030);
|
|
|
|
|
|
|
|
|
|
if ( !outbuf ) {
|
|
|
|
|
printf("out of memory!\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = fread(outbuf+0x1030, 1, length, file);
|
|
|
|
|
if(len < length) {
|
|
|
|
|
perror(iname);
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(outbuf, 0, 0x1030);
|
|
|
|
|
strcpy((char *)outbuf, "COWON_X5_FW");
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < length;i++)
|
|
|
|
|
sum += outbuf[0x1030 + i];
|
|
|
|
|
|
|
|
|
|
int2be(length, &outbuf[0x1024]);
|
|
|
|
|
outbuf[0x102b] = sum;
|
|
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
|
|
file = fopen(oname, "wb");
|
|
|
|
|
if (!file) {
|
|
|
|
|
perror(oname);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = fwrite(outbuf, 1, length+0x1030, file);
|
|
|
|
|
if(len < length) {
|
|
|
|
|
perror(oname);
|
|
|
|
|
return -4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
}
|