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>
|
|
|
|
|
|
|
|
|
|
int main (int argc, char** argv)
|
|
|
|
|
{
|
|
|
|
|
unsigned long length,i,slen;
|
|
|
|
|
unsigned char *inbuf,*outbuf;
|
|
|
|
|
unsigned short crc=0;
|
2002-12-19 15:00:40 +00:00
|
|
|
|
unsigned char header[24];
|
|
|
|
|
unsigned char *iname = argv[1];
|
|
|
|
|
unsigned char *oname = argv[2];
|
|
|
|
|
int headerlen = 6;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
FILE* file;
|
2003-10-27 18:04:28 +00:00
|
|
|
|
int version;
|
2003-12-12 14:07:28 +00:00
|
|
|
|
int scramble=1;
|
2002-03-28 14:34:13 +00:00
|
|
|
|
|
|
|
|
|
if (argc < 3) {
|
2003-12-12 14:07:28 +00:00
|
|
|
|
printf("usage: %s [-fm] [-v2] [-neo]<input file> <output file>\n",argv[0]);
|
2002-03-28 14:34:13 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2002-12-19 15:00:40 +00:00
|
|
|
|
}
|
2003-12-12 14:07:28 +00:00
|
|
|
|
|
|
|
|
|
else if(!strcmp(argv[1], "-neo")) {
|
|
|
|
|
headerlen = 17;
|
|
|
|
|
iname = argv[2];
|
|
|
|
|
oname = argv[3];
|
|
|
|
|
scramble = 0;
|
|
|
|
|
}
|
|
|
|
|
|
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 */
|
|
|
|
|
|
2003-04-14 09:59:47 +00:00
|
|
|
|
if ((length + headerlen) >= 0x32000) {
|
|
|
|
|
printf("error: max firmware size is 200KB!\n");
|
|
|
|
|
fclose(file);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-28 14:34:13 +00:00
|
|
|
|
fseek(file,0,SEEK_SET);
|
|
|
|
|
inbuf = malloc(length);
|
|
|
|
|
outbuf = malloc(length);
|
|
|
|
|
if ( !inbuf || !outbuf ) {
|
|
|
|
|
printf("out of memory!\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
if(scramble) {
|
|
|
|
|
/* 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;
|
|
|
|
|
}
|
2002-03-28 14:34:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* calculate checksum */
|
|
|
|
|
for (i=0;i<length;i++)
|
|
|
|
|
crc += inbuf[i];
|
|
|
|
|
|
2002-12-19 15:00:40 +00:00
|
|
|
|
memset(header, 0, sizeof header);
|
2003-12-12 14:07:28 +00:00
|
|
|
|
if(scramble) {
|
|
|
|
|
if (headerlen == 6) {
|
|
|
|
|
header[0] = (length >> 24) & 0xff;
|
|
|
|
|
header[1] = (length >> 16) & 0xff;
|
|
|
|
|
header[2] = (length >> 8) & 0xff;
|
|
|
|
|
header[3] = length & 0xff;
|
|
|
|
|
header[4] = (crc >> 8) & 0xff;
|
|
|
|
|
header[5] = crc & 0xff;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
header[0] =
|
|
|
|
|
header[1] =
|
|
|
|
|
header[2] =
|
|
|
|
|
header[3] = 0xff; /* ??? */
|
2002-12-19 15:00:40 +00:00
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
header[6] = (crc >> 8) & 0xff;
|
|
|
|
|
header[7] = crc & 0xff;
|
|
|
|
|
|
|
|
|
|
header[11] = version;
|
2002-12-19 15:00:40 +00:00
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
header[15] = headerlen; /* really? */
|
2002-12-19 15:00:40 +00:00
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
header[20] = (length >> 24) & 0xff;
|
|
|
|
|
header[21] = (length >> 16) & 0xff;
|
|
|
|
|
header[22] = (length >> 8) & 0xff;
|
|
|
|
|
header[23] = length & 0xff;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
#define MY_FIRMWARE_TYPE "Rockbox"
|
|
|
|
|
#define MY_HEADER_VERSION 1
|
2002-12-19 15:00:40 +00:00
|
|
|
|
|
2003-12-12 14:07:28 +00:00
|
|
|
|
strncpy(header,MY_FIRMWARE_TYPE,9);
|
|
|
|
|
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;
|
|
|
|
|
header[13]=(sizeof(header)>>24)&0xFF;
|
|
|
|
|
header[14]=(sizeof(header)>>16)&0xFF;
|
|
|
|
|
header[15]=(sizeof(header)>>8)&0xFF;
|
|
|
|
|
header[16]=sizeof(header)&0xFF;
|
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
|
|
|
|
}
|