1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include <sys/types.h>
34 static unsigned char* int2le(unsigned int val
)
36 static unsigned char addr
[4];
38 addr
[1] = (val
>> 8) & 0xff;
39 addr
[2] = (val
>> 16) & 0xff;
40 addr
[3] = (val
>> 24) & 0xff;
44 static unsigned int le2int(unsigned char* buf
)
46 unsigned int res
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
51 unsigned int _filesize(FILE* fd
)
53 unsigned int tmp
, oldpos
;
55 fseek(fd
, 0, SEEK_END
);
57 fseek(fd
, oldpos
, SEEK_SET
);
61 static void print_usage(void)
64 fprintf(stderr
, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
65 fprintf(stderr
, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
67 fprintf(stderr
, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
68 fprintf(stderr
, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n");
72 static int checksum(FILE *file
)
74 int oldpos
= ftell(file
);
75 int ret
=0, i
, filesize
= _filesize(file
)-0x40;
78 buf
= (unsigned char*)malloc(filesize
);
82 fseek(file
, oldpos
, SEEK_SET
);
83 fprintf(stderr
, "[ERR] Error while allocating memory\n");
87 fseek(file
, 0x40, SEEK_SET
);
88 if(fread(buf
, filesize
, 1, file
) != 1)
91 fseek(file
, oldpos
, SEEK_SET
);
92 fprintf(stderr
, "[ERR] Error while reading from file\n");
96 fprintf(stderr
, "[INFO] Computing checksum...");
98 for(i
= 0; i
< filesize
; i
+=4)
99 ret
+= le2int(&buf
[i
]);
102 fseek(file
, oldpos
, SEEK_SET
);
104 fprintf(stderr
, " Done!\n");
108 int main(int argc
, char *argv
[])
110 FILE *infile
, *outfile
, *fw
;
112 fprintf(stderr
, "HXFreplace v" VERSION
" - (C) 2008 Maurus Cuelenaere\n");
113 fprintf(stderr
, "This is free software; see the source for copying conditions. There is NO\n");
114 fprintf(stderr
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
122 if((infile
= fopen(argv
[1], "rb")) == NULL
)
124 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[1]);
128 if(fseek(infile
, 0x40, SEEK_SET
) != 0)
130 fprintf(stderr
, "[ERR] Cannot seek to 0x40\n");
135 fprintf(stderr
, "[INFO] Searching for ccpmp.bin...\n");
140 unsigned char tmp
[4];
142 #define READ(x, len) if(fread(x, len, 1, infile) != 1) \
144 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
151 filenamesize
= le2int(tmp
);
152 filename
= (char*)malloc(filenamesize
);
153 READ(filename
, filenamesize
);
154 if(strcmp(filename
, "ccpmp.bin") == 0)
155 found
= ftell(infile
);
159 fseek(infile
, le2int(tmp
), SEEK_CUR
);
164 fprintf(stderr
, "[INFO] Found ccpmp.bin at 0x%x\n", found
);
166 if((outfile
= fopen(argv
[2], "wb+")) == NULL
)
169 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[2]);
173 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
175 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
182 unsigned char* buffer
;
184 buffer
= (unsigned char*)malloc(found
);
185 fseek(infile
, 0, SEEK_SET
);
187 WRITE(buffer
, found
);
190 if((fw
= fopen(argv
[3], "rb")) == NULL
)
194 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[3]);
197 int fw_filesize
= _filesize(fw
);
199 #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
201 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
206 buffer
= (unsigned char*)malloc(fw_filesize
);
207 READ2(buffer
, fw_filesize
);
208 fputc(0x20, outfile
); /* Padding */
209 WRITE(int2le(fw_filesize
), 4);
210 WRITE(buffer
, fw_filesize
);
215 fseek(infile
, found
+1, SEEK_SET
);
217 if(fseek(infile
, le2int(&tmp
[0]), SEEK_CUR
) != 0)
219 fprintf(stderr
, "[INFO] Cannot seek into %s\n", argv
[1]);
224 found
= ftell(infile
);
226 int other_size
= _filesize(infile
) - found
;
227 buffer
= (unsigned char*)malloc(other_size
);
228 READ(buffer
, other_size
);
229 WRITE(buffer
, other_size
);
234 fseek(outfile
, 0x14, SEEK_SET
);
235 WRITE(int2le(_filesize(outfile
)), 4);
236 WRITE(int2le(checksum(outfile
)), 4);
239 fprintf(stderr
, "[INFO] Done!\n");