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];
52 #define PATH_SEPARATOR "\\"
54 #define PATH_SEPARATOR "/"
59 #define MIN(a, b) (a > b ? b : a)
60 static char* replace(char* str
)
63 memcpy(tmp
, str
, MIN(strlen(str
), 255));
67 if(*ptr
== 0x2F) /* /*/
75 static bool is_dir(const char* name1
, const char* name2
)
79 name
= (char*)malloc(strlen(name1
)+strlen(name2
)+1);
82 directory
= opendir(name
);
93 unsigned int _filesize(FILE* fd
)
95 unsigned int tmp
, oldpos
;
97 fseek(fd
, 0, SEEK_END
);
99 fseek(fd
, oldpos
, SEEK_SET
);
102 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
104 closedir(indir_handle); \
107 fprintf(stderr, "[ERR] Error writing to file\n"); \
110 static void merge_hxf(const char* indir
, FILE* outfile
, const char* add
)
118 if((indir_handle
= opendir(dir
)) == NULL
)
120 fprintf(stderr
, "[ERR] Error opening dir %s\n", indir
);
124 while((dirs
= readdir(indir_handle
)) != NULL
)
126 if(strcmp(dirs
->d_name
, "..") != 0 &&
127 strcmp(dirs
->d_name
, ".") != 0)
129 fprintf(stderr
, "[INFO] %s\%s\n", add
, dirs
->d_name
);
130 if(is_dir(dir
, dirs
->d_name
))
134 strcat(dir2
, dirs
->d_name
);
135 strcat(dir2
, PATH_SEPARATOR
);
136 merge_hxf(indir
, outfile
, dir2
);
141 unsigned char *buffer
;
143 unsigned int filesize
;
145 strcat(file
, dirs
->d_name
);
146 if((filehandle
= fopen(file
, "rb")) == NULL
)
148 fprintf(stderr
, "[ERR] Cannot open %s\n", file
);
149 closedir(indir_handle
);
152 filesize
= _filesize(filehandle
);
155 buffer
= (unsigned char*)malloc(filesize
);
159 closedir(indir_handle
);
160 fprintf(stderr
, "[ERR] Cannot allocate memory\n");
163 if(fread(buffer
, filesize
, 1, filehandle
) != 1)
166 closedir(indir_handle
);
168 fprintf(stderr
, "[ERR] Cannot read from %s%s%s\n", add
, PATH_SEPARATOR
, dirs
->d_name
);
176 #ifdef _DIRENT_HAVE_D_NAMLEN
177 WRITE(int2le(dirs
->d_namlen
+strlen(add
)), 4);
179 WRITE(int2le(strlen(dirs
->d_name
)+strlen(add
)), 4);
182 WRITE(replace((char*)add
), strlen(add
)-1);
184 WRITE(add
, strlen(add
)-1);
186 WRITE(PATH_SEPARATOR
, 1);
187 #ifdef _DIRENT_HAVE_D_NAMLEN
188 WRITE(dirs
->d_name
, dirs
->d_namlen
);
190 WRITE(dirs
->d_name
, strlen(dirs
->d_name
));
195 #ifdef _DIRENT_HAVE_D_NAMLEN
196 WRITE(int2le(dirs
->d_namlen
), 4);
197 WRITE(dirs
->d_name
, dirs
->d_namlen
);
199 WRITE(int2le(strlen(dirs
->d_name
)), 4);
200 WRITE(dirs
->d_name
, strlen(dirs
->d_name
));
203 WRITE(int2le(filesize
), 4);
206 WRITE(buffer
, filesize
);
212 closedir(indir_handle
);
215 static void print_usage(void)
218 fprintf(stderr
, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n");
219 fprintf(stderr
, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n");
221 fprintf(stderr
, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n");
222 fprintf(stderr
, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n");
226 static int checksum(FILE *file
)
228 int oldpos
= ftell(file
);
229 int ret
=0, i
, filesize
= _filesize(file
)-0x40;
232 buf
= (unsigned char*)malloc(filesize
);
236 fseek(file
, oldpos
, SEEK_SET
);
237 fprintf(stderr
, "[ERR] Error while allocating memory\n");
241 fseek(file
, 0x40, SEEK_SET
);
242 if(fread(buf
, filesize
, 1, file
) != 1)
245 fseek(file
, oldpos
, SEEK_SET
);
246 fprintf(stderr
, "[ERR] Error while reading from file\n");
250 fprintf(stderr
, "[INFO] Computing checksum...");
252 for(i
= 0; i
< filesize
; i
+=4)
253 ret
+= le2int(&buf
[i
]);
256 fseek(file
, oldpos
, SEEK_SET
);
258 fprintf(stderr
, " Done!\n");
262 int main(int argc
, char *argv
[])
266 fprintf(stderr
, "HXFmerge v" VERSION
" - (C) 2008 Maurus Cuelenaere\n");
267 fprintf(stderr
, "This is free software; see the source for copying conditions. There is NO\n");
268 fprintf(stderr
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
277 if(strcmp((char*)(argv
[1]+strlen(argv
[1])-1), "\\") != 0)
279 fprintf(stderr
, "[ERR] Input path must end with a \\\n");
281 if(strcmp((char*)(argv
[1]+strlen(argv
[1])-1), "/") != 0)
283 fprintf(stderr
, "[ERR] Input path must end with a /\n");
288 if((outfile
= fopen(argv
[2], "wb+")) == NULL
)
290 fprintf(stderr
, "[ERR] Cannot open %s\n", argv
[2]);
294 fseek(outfile
, 0x40, SEEK_SET
);
296 merge_hxf(argv
[1], outfile
, "");
300 fprintf(stderr
, "[INFO] Filling header...\n");
303 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
305 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \
310 fseek(outfile
, 0, SEEK_SET
);
311 WRITE("WADF0100200804111437", 20);
312 WRITE(int2le(_filesize(outfile
)), 4);
313 WRITE(int2le(checksum(outfile
)), 4);
315 WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32);
318 fprintf(stderr
, "[INFO] Done!\n");