summaryrefslogtreecommitdiff
path: root/utils/jz4740_tools
diff options
context:
space:
mode:
Diffstat (limited to 'utils/jz4740_tools')
-rw-r--r--utils/jz4740_tools/HXFmerge.c642
-rw-r--r--utils/jz4740_tools/HXFreplace.c484
-rw-r--r--utils/jz4740_tools/HXFsplit.c642
3 files changed, 884 insertions, 884 deletions
diff --git a/utils/jz4740_tools/HXFmerge.c b/utils/jz4740_tools/HXFmerge.c
index 6b6c62505e..fdd81b8445 100644
--- a/utils/jz4740_tools/HXFmerge.c
+++ b/utils/jz4740_tools/HXFmerge.c
@@ -1,321 +1,321 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere 10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <unistd.h> 27#include <unistd.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <stdbool.h> 29#include <stdbool.h>
30#include <dirent.h> 30#include <dirent.h>
31 31
32#define VERSION "0.2" 32#define VERSION "0.2"
33 33
34static unsigned char* int2le(unsigned int val) 34static unsigned char* int2le(unsigned int val)
35{ 35{
36 static unsigned char addr[4]; 36 static unsigned char addr[4];
37 addr[0] = val & 0xff; 37 addr[0] = val & 0xff;
38 addr[1] = (val >> 8) & 0xff; 38 addr[1] = (val >> 8) & 0xff;
39 addr[2] = (val >> 16) & 0xff; 39 addr[2] = (val >> 16) & 0xff;
40 addr[3] = (val >> 24) & 0xff; 40 addr[3] = (val >> 24) & 0xff;
41 return addr; 41 return addr;
42} 42}
43 43
44static unsigned int le2int(unsigned char* buf) 44static unsigned int le2int(unsigned char* buf)
45{ 45{
46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
47 47
48 return res; 48 return res;
49} 49}
50 50
51#ifdef _WIN32 51#ifdef _WIN32
52 #define PATH_SEPARATOR "\\" 52 #define PATH_SEPARATOR "\\"
53#else 53#else
54 #define PATH_SEPARATOR "/" 54 #define PATH_SEPARATOR "/"
55#endif 55#endif
56 56
57#ifndef _WIN32 57#ifndef _WIN32
58 58
59#define MIN(a, b) (a > b ? b : a) 59#define MIN(a, b) (a > b ? b : a)
60static char* replace(char* str) 60static char* replace(char* str)
61{ 61{
62 static char tmp[255]; 62 static char tmp[255];
63 memcpy(tmp, str, MIN(strlen(str), 255)); 63 memcpy(tmp, str, MIN(strlen(str), 255));
64 char *ptr = tmp; 64 char *ptr = tmp;
65 while(*ptr != 0) 65 while(*ptr != 0)
66 { 66 {
67 if(*ptr == 0x2F) /* /*/ 67 if(*ptr == 0x2F) /* /*/
68 *ptr = 0x5C; /* \ */ 68 *ptr = 0x5C; /* \ */
69 ptr++; 69 ptr++;
70 } 70 }
71 return tmp; 71 return tmp;
72} 72}
73#endif 73#endif
74 74
75static bool is_dir(const char* name1, const char* name2) 75static bool is_dir(const char* name1, const char* name2)
76{ 76{
77 char *name; 77 char *name;
78 DIR *directory; 78 DIR *directory;
79 name = (char*)malloc(strlen(name1)+strlen(name2)+1); 79 name = (char*)malloc(strlen(name1)+strlen(name2)+1);
80 strcpy(name, name1); 80 strcpy(name, name1);
81 strcat(name, name2); 81 strcat(name, name2);
82 directory = opendir(name); 82 directory = opendir(name);
83 free(name); 83 free(name);
84 if(directory) 84 if(directory)
85 { 85 {
86 closedir(directory); 86 closedir(directory);
87 return true; 87 return true;
88 } 88 }
89 else 89 else
90 return false; 90 return false;
91} 91}
92 92
93unsigned int _filesize(FILE* fd) 93unsigned int _filesize(FILE* fd)
94{ 94{
95 unsigned int tmp, oldpos; 95 unsigned int tmp, oldpos;
96 oldpos = ftell(fd); 96 oldpos = ftell(fd);
97 fseek(fd, 0, SEEK_END); 97 fseek(fd, 0, SEEK_END);
98 tmp = ftell(fd); 98 tmp = ftell(fd);
99 fseek(fd, oldpos, SEEK_SET); 99 fseek(fd, oldpos, SEEK_SET);
100 return tmp; 100 return tmp;
101} 101}
102#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ 102#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
103 { \ 103 { \
104 closedir(indir_handle); \ 104 closedir(indir_handle); \
105 if(filesize > 0) \ 105 if(filesize > 0) \
106 free(buffer); \ 106 free(buffer); \
107 fprintf(stderr, "[ERR] Error writing to file\n"); \ 107 fprintf(stderr, "[ERR] Error writing to file\n"); \
108 return; \ 108 return; \
109 } 109 }
110static void merge_hxf(const char* indir, FILE* outfile, const char* add) 110static void merge_hxf(const char* indir, FILE* outfile, const char* add)
111{ 111{
112 DIR *indir_handle; 112 DIR *indir_handle;
113 struct dirent *dirs; 113 struct dirent *dirs;
114 char dir[255]; 114 char dir[255];
115 strcpy(dir, indir); 115 strcpy(dir, indir);
116 strcat(dir, add); 116 strcat(dir, add);
117 117
118 if((indir_handle = opendir(dir)) == NULL) 118 if((indir_handle = opendir(dir)) == NULL)
119 { 119 {
120 fprintf(stderr, "[ERR] Error opening dir %s\n", indir); 120 fprintf(stderr, "[ERR] Error opening dir %s\n", indir);
121 return; 121 return;
122 } 122 }
123 123
124 while((dirs = readdir(indir_handle)) != NULL) 124 while((dirs = readdir(indir_handle)) != NULL)
125 { 125 {
126 if(strcmp(dirs->d_name, "..") != 0 && 126 if(strcmp(dirs->d_name, "..") != 0 &&
127 strcmp(dirs->d_name, ".") != 0) 127 strcmp(dirs->d_name, ".") != 0)
128 { 128 {
129 fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name); 129 fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name);
130 if(is_dir(dir, dirs->d_name)) 130 if(is_dir(dir, dirs->d_name))
131 { 131 {
132 char dir2[255]; 132 char dir2[255];
133 strcpy(dir2, add); 133 strcpy(dir2, add);
134 strcat(dir2, dirs->d_name); 134 strcat(dir2, dirs->d_name);
135 strcat(dir2, PATH_SEPARATOR); 135 strcat(dir2, PATH_SEPARATOR);
136 merge_hxf(indir, outfile, dir2); 136 merge_hxf(indir, outfile, dir2);
137 } 137 }
138 else 138 else
139 { 139 {
140 FILE *filehandle; 140 FILE *filehandle;
141 unsigned char *buffer; 141 unsigned char *buffer;
142 char file[255]; 142 char file[255];
143 unsigned int filesize; 143 unsigned int filesize;
144 strcpy(file, dir); 144 strcpy(file, dir);
145 strcat(file, dirs->d_name); 145 strcat(file, dirs->d_name);
146 if((filehandle = fopen(file, "rb")) == NULL) 146 if((filehandle = fopen(file, "rb")) == NULL)
147 { 147 {
148 fprintf(stderr, "[ERR] Cannot open %s\n", file); 148 fprintf(stderr, "[ERR] Cannot open %s\n", file);
149 closedir(indir_handle); 149 closedir(indir_handle);
150 return; 150 return;
151 } 151 }
152 filesize = _filesize(filehandle); 152 filesize = _filesize(filehandle);
153 if(filesize > 0) 153 if(filesize > 0)
154 { 154 {
155 buffer = (unsigned char*)malloc(filesize); 155 buffer = (unsigned char*)malloc(filesize);
156 if(buffer == NULL) 156 if(buffer == NULL)
157 { 157 {
158 fclose(filehandle); 158 fclose(filehandle);
159 closedir(indir_handle); 159 closedir(indir_handle);
160 fprintf(stderr, "[ERR] Cannot allocate memory\n"); 160 fprintf(stderr, "[ERR] Cannot allocate memory\n");
161 return; 161 return;
162 } 162 }
163 if(fread(buffer, filesize, 1, filehandle) != 1) 163 if(fread(buffer, filesize, 1, filehandle) != 1)
164 { 164 {
165 fclose(filehandle); 165 fclose(filehandle);
166 closedir(indir_handle); 166 closedir(indir_handle);
167 free(buffer); 167 free(buffer);
168 fprintf(stderr, "[ERR] Cannot read from %s%s%s\n", add, PATH_SEPARATOR, dirs->d_name); 168 fprintf(stderr, "[ERR] Cannot read from %s%s%s\n", add, PATH_SEPARATOR, dirs->d_name);
169 return; 169 return;
170 } 170 }
171 } 171 }
172 fclose(filehandle); 172 fclose(filehandle);
173 173
174 if(strlen(add)>0) 174 if(strlen(add)>0)
175 { 175 {
176#ifdef _DIRENT_HAVE_D_NAMLEN 176#ifdef _DIRENT_HAVE_D_NAMLEN
177 WRITE(int2le(dirs->d_namlen+strlen(add)), 4); 177 WRITE(int2le(dirs->d_namlen+strlen(add)), 4);
178#else 178#else
179 WRITE(int2le(strlen(dirs->d_name)+strlen(add)), 4); 179 WRITE(int2le(strlen(dirs->d_name)+strlen(add)), 4);
180#endif 180#endif
181#ifndef _WIN32 181#ifndef _WIN32
182 WRITE(replace((char*)add), strlen(add)-1); 182 WRITE(replace((char*)add), strlen(add)-1);
183#else 183#else
184 WRITE(add, strlen(add)-1); 184 WRITE(add, strlen(add)-1);
185#endif 185#endif
186 WRITE(PATH_SEPARATOR, 1); 186 WRITE(PATH_SEPARATOR, 1);
187#ifdef _DIRENT_HAVE_D_NAMLEN 187#ifdef _DIRENT_HAVE_D_NAMLEN
188 WRITE(dirs->d_name, dirs->d_namlen); 188 WRITE(dirs->d_name, dirs->d_namlen);
189#else 189#else
190 WRITE(dirs->d_name, strlen(dirs->d_name)); 190 WRITE(dirs->d_name, strlen(dirs->d_name));
191#endif 191#endif
192 } 192 }
193 else 193 else
194 { 194 {
195#ifdef _DIRENT_HAVE_D_NAMLEN 195#ifdef _DIRENT_HAVE_D_NAMLEN
196 WRITE(int2le(dirs->d_namlen), 4); 196 WRITE(int2le(dirs->d_namlen), 4);
197 WRITE(dirs->d_name, dirs->d_namlen); 197 WRITE(dirs->d_name, dirs->d_namlen);
198#else 198#else
199 WRITE(int2le(strlen(dirs->d_name)), 4); 199 WRITE(int2le(strlen(dirs->d_name)), 4);
200 WRITE(dirs->d_name, strlen(dirs->d_name)); 200 WRITE(dirs->d_name, strlen(dirs->d_name));
201#endif 201#endif
202 } 202 }
203 WRITE(int2le(filesize), 4); 203 WRITE(int2le(filesize), 4);
204 if(filesize>0) 204 if(filesize>0)
205 { 205 {
206 WRITE(buffer, filesize); 206 WRITE(buffer, filesize);
207 free(buffer); 207 free(buffer);
208 } 208 }
209 } 209 }
210 } 210 }
211 } 211 }
212 closedir(indir_handle); 212 closedir(indir_handle);
213} 213}
214 214
215static void print_usage(void) 215static void print_usage(void)
216{ 216{
217#ifdef _WIN32 217#ifdef _WIN32
218 fprintf(stderr, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n"); 218 fprintf(stderr, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n");
219 fprintf(stderr, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n"); 219 fprintf(stderr, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n");
220#else 220#else
221 fprintf(stderr, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n"); 221 fprintf(stderr, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n");
222 fprintf(stderr, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n"); 222 fprintf(stderr, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n");
223#endif 223#endif
224} 224}
225 225
226static int checksum(FILE *file) 226static int checksum(FILE *file)
227{ 227{
228 int oldpos = ftell(file); 228 int oldpos = ftell(file);
229 int ret=0, i, filesize = _filesize(file)-0x40; 229 int ret=0, i, filesize = _filesize(file)-0x40;
230 unsigned char *buf; 230 unsigned char *buf;
231 231
232 buf = (unsigned char*)malloc(filesize); 232 buf = (unsigned char*)malloc(filesize);
233 233
234 if(buf == NULL) 234 if(buf == NULL)
235 { 235 {
236 fseek(file, oldpos, SEEK_SET); 236 fseek(file, oldpos, SEEK_SET);
237 fprintf(stderr, "[ERR] Error while allocating memory\n"); 237 fprintf(stderr, "[ERR] Error while allocating memory\n");
238 return 0; 238 return 0;
239 } 239 }
240 240
241 fseek(file, 0x40, SEEK_SET); 241 fseek(file, 0x40, SEEK_SET);
242 if(fread(buf, filesize, 1, file) != 1) 242 if(fread(buf, filesize, 1, file) != 1)
243 { 243 {
244 free(buf); 244 free(buf);
245 fseek(file, oldpos, SEEK_SET); 245 fseek(file, oldpos, SEEK_SET);
246 fprintf(stderr, "[ERR] Error while reading from file\n"); 246 fprintf(stderr, "[ERR] Error while reading from file\n");
247 return 0; 247 return 0;
248 } 248 }
249 249
250 fprintf(stderr, "[INFO] Computing checksum..."); 250 fprintf(stderr, "[INFO] Computing checksum...");
251 251
252 for(i = 0; i < filesize; i+=4) 252 for(i = 0; i < filesize; i+=4)
253 ret += le2int(&buf[i]); 253 ret += le2int(&buf[i]);
254 254
255 free(buf); 255 free(buf);
256 fseek(file, oldpos, SEEK_SET); 256 fseek(file, oldpos, SEEK_SET);
257 257
258 fprintf(stderr, " Done!\n"); 258 fprintf(stderr, " Done!\n");
259 return ret; 259 return ret;
260} 260}
261 261
262int main(int argc, char *argv[]) 262int main(int argc, char *argv[])
263{ 263{
264 FILE *outfile; 264 FILE *outfile;
265 265
266 fprintf(stderr, "HXFmerge v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); 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"); 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"); 268 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
269 269
270 if(argc != 3) 270 if(argc != 3)
271 { 271 {
272 print_usage(); 272 print_usage();
273 return 1; 273 return 1;
274 } 274 }
275 275
276#ifdef _WIN32 276#ifdef _WIN32
277 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0) 277 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0)
278 { 278 {
279 fprintf(stderr, "[ERR] Input path must end with a \\\n"); 279 fprintf(stderr, "[ERR] Input path must end with a \\\n");
280#else 280#else
281 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0) 281 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0)
282 { 282 {
283 fprintf(stderr, "[ERR] Input path must end with a /\n"); 283 fprintf(stderr, "[ERR] Input path must end with a /\n");
284#endif 284#endif
285 return 2; 285 return 2;
286 } 286 }
287 287
288 if((outfile = fopen(argv[2], "wb+")) == NULL) 288 if((outfile = fopen(argv[2], "wb+")) == NULL)
289 { 289 {
290 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); 290 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
291 return 3; 291 return 3;
292 } 292 }
293 293
294 fseek(outfile, 0x40, SEEK_SET); 294 fseek(outfile, 0x40, SEEK_SET);
295 295
296 merge_hxf(argv[1], outfile, ""); 296 merge_hxf(argv[1], outfile, "");
297 297
298 fflush(outfile); 298 fflush(outfile);
299 299
300 fprintf(stderr, "[INFO] Filling header...\n"); 300 fprintf(stderr, "[INFO] Filling header...\n");
301 301
302#undef WRITE 302#undef WRITE
303#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ 303#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
304 { \ 304 { \
305 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \ 305 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \
306 fclose(outfile); \ 306 fclose(outfile); \
307 return 4; \ 307 return 4; \
308 } 308 }
309 fflush(outfile); 309 fflush(outfile);
310 fseek(outfile, 0, SEEK_SET); 310 fseek(outfile, 0, SEEK_SET);
311 WRITE("WADF0100200804111437", 20); 311 WRITE("WADF0100200804111437", 20);
312 WRITE(int2le(_filesize(outfile)), 4); 312 WRITE(int2le(_filesize(outfile)), 4);
313 WRITE(int2le(checksum(outfile)), 4); 313 WRITE(int2le(checksum(outfile)), 4);
314 WRITE(int2le(0), 4); 314 WRITE(int2le(0), 4);
315 WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32); 315 WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32);
316 fclose(outfile); 316 fclose(outfile);
317 317
318 fprintf(stderr, "[INFO] Done!\n"); 318 fprintf(stderr, "[INFO] Done!\n");
319 319
320 return 0; 320 return 0;
321} 321}
diff --git a/utils/jz4740_tools/HXFreplace.c b/utils/jz4740_tools/HXFreplace.c
index 989a59dd61..bcfff82540 100644
--- a/utils/jz4740_tools/HXFreplace.c
+++ b/utils/jz4740_tools/HXFreplace.c
@@ -1,242 +1,242 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere 10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <unistd.h> 27#include <unistd.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <stdbool.h> 29#include <stdbool.h>
30#include <dirent.h> 30#include <dirent.h>
31 31
32#define VERSION "0.1" 32#define VERSION "0.1"
33 33
34static unsigned char* int2le(unsigned int val) 34static unsigned char* int2le(unsigned int val)
35{ 35{
36 static unsigned char addr[4]; 36 static unsigned char addr[4];
37 addr[0] = val & 0xff; 37 addr[0] = val & 0xff;
38 addr[1] = (val >> 8) & 0xff; 38 addr[1] = (val >> 8) & 0xff;
39 addr[2] = (val >> 16) & 0xff; 39 addr[2] = (val >> 16) & 0xff;
40 addr[3] = (val >> 24) & 0xff; 40 addr[3] = (val >> 24) & 0xff;
41 return addr; 41 return addr;
42} 42}
43 43
44static unsigned int le2int(unsigned char* buf) 44static unsigned int le2int(unsigned char* buf)
45{ 45{
46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
47 47
48 return res; 48 return res;
49} 49}
50 50
51unsigned int _filesize(FILE* fd) 51unsigned int _filesize(FILE* fd)
52{ 52{
53 unsigned int tmp, oldpos; 53 unsigned int tmp, oldpos;
54 oldpos = ftell(fd); 54 oldpos = ftell(fd);
55 fseek(fd, 0, SEEK_END); 55 fseek(fd, 0, SEEK_END);
56 tmp = ftell(fd); 56 tmp = ftell(fd);
57 fseek(fd, oldpos, SEEK_SET); 57 fseek(fd, oldpos, SEEK_SET);
58 return tmp; 58 return tmp;
59} 59}
60 60
61static void print_usage(void) 61static void print_usage(void)
62{ 62{
63#ifdef _WIN32 63#ifdef _WIN32
64 fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); 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"); 65 fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
66#else 66#else
67 fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\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"); 68 fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n");
69#endif 69#endif
70} 70}
71 71
72static int checksum(FILE *file) 72static int checksum(FILE *file)
73{ 73{
74 int oldpos = ftell(file); 74 int oldpos = ftell(file);
75 int ret=0, i, filesize = _filesize(file)-0x40; 75 int ret=0, i, filesize = _filesize(file)-0x40;
76 unsigned char *buf; 76 unsigned char *buf;
77 77
78 buf = (unsigned char*)malloc(filesize); 78 buf = (unsigned char*)malloc(filesize);
79 79
80 if(buf == NULL) 80 if(buf == NULL)
81 { 81 {
82 fseek(file, oldpos, SEEK_SET); 82 fseek(file, oldpos, SEEK_SET);
83 fprintf(stderr, "[ERR] Error while allocating memory\n"); 83 fprintf(stderr, "[ERR] Error while allocating memory\n");
84 return 0; 84 return 0;
85 } 85 }
86 86
87 fseek(file, 0x40, SEEK_SET); 87 fseek(file, 0x40, SEEK_SET);
88 if(fread(buf, filesize, 1, file) != 1) 88 if(fread(buf, filesize, 1, file) != 1)
89 { 89 {
90 free(buf); 90 free(buf);
91 fseek(file, oldpos, SEEK_SET); 91 fseek(file, oldpos, SEEK_SET);
92 fprintf(stderr, "[ERR] Error while reading from file\n"); 92 fprintf(stderr, "[ERR] Error while reading from file\n");
93 return 0; 93 return 0;
94 } 94 }
95 95
96 fprintf(stderr, "[INFO] Computing checksum..."); 96 fprintf(stderr, "[INFO] Computing checksum...");
97 97
98 for(i = 0; i < filesize; i+=4) 98 for(i = 0; i < filesize; i+=4)
99 ret += le2int(&buf[i]); 99 ret += le2int(&buf[i]);
100 100
101 free(buf); 101 free(buf);
102 fseek(file, oldpos, SEEK_SET); 102 fseek(file, oldpos, SEEK_SET);
103 103
104 fprintf(stderr, " Done!\n"); 104 fprintf(stderr, " Done!\n");
105 return ret; 105 return ret;
106} 106}
107 107
108int main(int argc, char *argv[]) 108int main(int argc, char *argv[])
109{ 109{
110 FILE *infile, *outfile, *fw; 110 FILE *infile, *outfile, *fw;
111 111
112 fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); 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"); 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"); 114 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
115 115
116 if(argc != 4) 116 if(argc != 4)
117 { 117 {
118 print_usage(); 118 print_usage();
119 return 1; 119 return 1;
120 } 120 }
121 121
122 if((infile = fopen(argv[1], "rb")) == NULL) 122 if((infile = fopen(argv[1], "rb")) == NULL)
123 { 123 {
124 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); 124 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
125 return 2; 125 return 2;
126 } 126 }
127 127
128 if(fseek(infile, 0x40, SEEK_SET) != 0) 128 if(fseek(infile, 0x40, SEEK_SET) != 0)
129 { 129 {
130 fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); 130 fprintf(stderr, "[ERR] Cannot seek to 0x40\n");
131 fclose(infile); 131 fclose(infile);
132 return 3; 132 return 3;
133 } 133 }
134 134
135 fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); 135 fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n");
136 136
137 int found = -1; 137 int found = -1;
138 int filenamesize; 138 int filenamesize;
139 char *filename; 139 char *filename;
140 unsigned char tmp[4]; 140 unsigned char tmp[4];
141 141
142#define READ(x, len) if(fread(x, len, 1, infile) != 1) \ 142#define READ(x, len) if(fread(x, len, 1, infile) != 1) \
143 { \ 143 { \
144 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ 144 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
145 fclose(infile); \ 145 fclose(infile); \
146 return 4; \ 146 return 4; \
147 } 147 }
148 while(found < 0) 148 while(found < 0)
149 { 149 {
150 READ(&tmp[0], 4); 150 READ(&tmp[0], 4);
151 filenamesize = le2int(tmp); 151 filenamesize = le2int(tmp);
152 filename = (char*)malloc(filenamesize); 152 filename = (char*)malloc(filenamesize);
153 READ(filename, filenamesize); 153 READ(filename, filenamesize);
154 if(strcmp(filename, "ccpmp.bin") == 0) 154 if(strcmp(filename, "ccpmp.bin") == 0)
155 found = ftell(infile); 155 found = ftell(infile);
156 else 156 else
157 { 157 {
158 READ(&tmp[0], 4); 158 READ(&tmp[0], 4);
159 fseek(infile, le2int(tmp), SEEK_CUR); 159 fseek(infile, le2int(tmp), SEEK_CUR);
160 } 160 }
161 free(filename); 161 free(filename);
162 } 162 }
163 163
164 fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); 164 fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found);
165 165
166 if((outfile = fopen(argv[2], "wb+")) == NULL) 166 if((outfile = fopen(argv[2], "wb+")) == NULL)
167 { 167 {
168 fclose(infile); 168 fclose(infile);
169 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); 169 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
170 return 5; 170 return 5;
171 } 171 }
172 172
173#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ 173#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
174 { \ 174 { \
175 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ 175 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
176 fclose(outfile); \ 176 fclose(outfile); \
177 if(fw != NULL) \ 177 if(fw != NULL) \
178 fclose(fw); \ 178 fclose(fw); \
179 return 5; \ 179 return 5; \
180 } 180 }
181 181
182 unsigned char* buffer; 182 unsigned char* buffer;
183 183
184 buffer = (unsigned char*)malloc(found); 184 buffer = (unsigned char*)malloc(found);
185 fseek(infile, 0, SEEK_SET); 185 fseek(infile, 0, SEEK_SET);
186 READ(buffer, found); 186 READ(buffer, found);
187 WRITE(buffer, found); 187 WRITE(buffer, found);
188 free(buffer); 188 free(buffer);
189 189
190 if((fw = fopen(argv[3], "rb")) == NULL) 190 if((fw = fopen(argv[3], "rb")) == NULL)
191 { 191 {
192 fclose(infile); 192 fclose(infile);
193 fclose(outfile); 193 fclose(outfile);
194 fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); 194 fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]);
195 } 195 }
196 196
197 int fw_filesize = _filesize(fw); 197 int fw_filesize = _filesize(fw);
198 198
199#define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ 199#define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
200 { \ 200 { \
201 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ 201 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
202 fclose(infile); \ 202 fclose(infile); \
203 fclose(outfile); \ 203 fclose(outfile); \
204 return 6; \ 204 return 6; \
205 } 205 }
206 buffer = (unsigned char*)malloc(fw_filesize); 206 buffer = (unsigned char*)malloc(fw_filesize);
207 READ2(buffer, fw_filesize); 207 READ2(buffer, fw_filesize);
208 fputc(0x20, outfile); /* Padding */ 208 fputc(0x20, outfile); /* Padding */
209 WRITE(int2le(fw_filesize), 4); 209 WRITE(int2le(fw_filesize), 4);
210 WRITE(buffer, fw_filesize); 210 WRITE(buffer, fw_filesize);
211 free(buffer); 211 free(buffer);
212 fclose(fw); 212 fclose(fw);
213 fw = NULL; 213 fw = NULL;
214 214
215 fseek(infile, found+1, SEEK_SET); 215 fseek(infile, found+1, SEEK_SET);
216 READ(&tmp, 4); 216 READ(&tmp, 4);
217 if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) 217 if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0)
218 { 218 {
219 fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); 219 fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]);
220 fclose(infile); 220 fclose(infile);
221 fclose(outfile); 221 fclose(outfile);
222 return 7; 222 return 7;
223 } 223 }
224 found = ftell(infile); 224 found = ftell(infile);
225 225
226 int other_size = _filesize(infile) - found; 226 int other_size = _filesize(infile) - found;
227 buffer = (unsigned char*)malloc(other_size); 227 buffer = (unsigned char*)malloc(other_size);
228 READ(buffer, other_size); 228 READ(buffer, other_size);
229 WRITE(buffer, other_size); 229 WRITE(buffer, other_size);
230 free(buffer); 230 free(buffer);
231 fclose(infile); 231 fclose(infile);
232 232
233 fflush(outfile); 233 fflush(outfile);
234 fseek(outfile, 0x14, SEEK_SET); 234 fseek(outfile, 0x14, SEEK_SET);
235 WRITE(int2le(_filesize(outfile)), 4); 235 WRITE(int2le(_filesize(outfile)), 4);
236 WRITE(int2le(checksum(outfile)), 4); 236 WRITE(int2le(checksum(outfile)), 4);
237 fclose(outfile); 237 fclose(outfile);
238 238
239 fprintf(stderr, "[INFO] Done!\n"); 239 fprintf(stderr, "[INFO] Done!\n");
240 240
241 return 0; 241 return 0;
242} 242}
diff --git a/utils/jz4740_tools/HXFsplit.c b/utils/jz4740_tools/HXFsplit.c
index 6e945b067a..dbeace8a20 100644
--- a/utils/jz4740_tools/HXFsplit.c
+++ b/utils/jz4740_tools/HXFsplit.c
@@ -1,321 +1,321 @@
1/*************************************************************************** 1/***************************************************************************
2 * __________ __ ___. 2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere 10 * Copyright (C) 2008 by Maurus Cuelenaere
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2 14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. 15 * of the License, or (at your option) any later version.
16 * 16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h> 24#include <string.h>
25#include <sys/types.h> 25#include <sys/types.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <unistd.h> 27#include <unistd.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <stdbool.h> 29#include <stdbool.h>
30 30
31#define VERSION "0.2" 31#define VERSION "0.2"
32 32
33struct header{ 33struct header{
34 char main_header[20]; 34 char main_header[20];
35 unsigned int size; 35 unsigned int size;
36 unsigned int checksum; 36 unsigned int checksum;
37 unsigned int unknown; 37 unsigned int unknown;
38 char other_header[32]; 38 char other_header[32];
39}; 39};
40 40
41static char* basepath(char* path) 41static char* basepath(char* path)
42{ 42{
43 static char tmp[255]; 43 static char tmp[255];
44 char *ptr, *ptr2, *ptr3; 44 char *ptr, *ptr2, *ptr3;
45 ptr = path; 45 ptr = path;
46 ptr2 = (char*)tmp; 46 ptr2 = (char*)tmp;
47#ifdef _WIN32 47#ifdef _WIN32
48 ptr3 = strrchr(path, 0x5C); 48 ptr3 = strrchr(path, 0x5C);
49#else 49#else
50 ptr3 = strrchr(path, 0x2F); 50 ptr3 = strrchr(path, 0x2F);
51#endif 51#endif
52 while((int)ptr < (int)ptr3) 52 while((int)ptr < (int)ptr3)
53 { 53 {
54 *ptr2 = *ptr; 54 *ptr2 = *ptr;
55 ptr++; 55 ptr++;
56 ptr2++; 56 ptr2++;
57 } 57 }
58#ifdef _WIN32 58#ifdef _WIN32
59 *ptr2 = 0x5C; 59 *ptr2 = 0x5C;
60#else 60#else
61 *ptr2 = 0x2F; 61 *ptr2 = 0x2F;
62#endif 62#endif
63 ptr2++; 63 ptr2++;
64 *ptr2 = 0; 64 *ptr2 = 0;
65 return (char*)tmp; 65 return (char*)tmp;
66} 66}
67 67
68#ifndef _WIN32 68#ifndef _WIN32
69static void replace(char* str) 69static void replace(char* str)
70{ 70{
71 char *ptr = str; 71 char *ptr = str;
72 while(*ptr != 0) 72 while(*ptr != 0)
73 { 73 {
74 if(*ptr == 0x5C) /* \ */ 74 if(*ptr == 0x5C) /* \ */
75 *ptr = 0x2F; /* / */ 75 *ptr = 0x2F; /* / */
76 ptr++; 76 ptr++;
77 } 77 }
78} 78}
79#endif 79#endif
80 80
81static unsigned int le2int(unsigned char* buf) 81static unsigned int le2int(unsigned char* buf)
82{ 82{
83 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; 83 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
84 84
85 return res; 85 return res;
86} 86}
87 87
88#ifdef _WIN32 88#ifdef _WIN32
89 #define PATH_SEPARATOR '\\' 89 #define PATH_SEPARATOR '\\'
90#else 90#else
91 #define PATH_SEPARATOR '/' 91 #define PATH_SEPARATOR '/'
92#endif 92#endif
93 93
94static unsigned int __mkdir(const char *path) 94static unsigned int __mkdir(const char *path)
95{ 95{
96 char opath[256]; 96 char opath[256];
97 char *p; 97 char *p;
98 size_t len; 98 size_t len;
99 99
100 strncpy(opath, path, sizeof(opath)); 100 strncpy(opath, path, sizeof(opath));
101 len = strlen(opath); 101 len = strlen(opath);
102 if(opath[len - 1] == PATH_SEPARATOR) 102 if(opath[len - 1] == PATH_SEPARATOR)
103 opath[len - 1] = '\0'; 103 opath[len - 1] = '\0';
104 for(p = opath; *p; p++) 104 for(p = opath; *p; p++)
105 if(*p == PATH_SEPARATOR) 105 if(*p == PATH_SEPARATOR)
106 { 106 {
107 *p = '\0'; 107 *p = '\0';
108 if(access(opath, F_OK)) 108 if(access(opath, F_OK))
109#ifdef _WIN32 109#ifdef _WIN32
110 mkdir(opath); 110 mkdir(opath);
111#else 111#else
112 mkdir(opath, S_IRWXU); 112 mkdir(opath, S_IRWXU);
113#endif 113#endif
114 *p = PATH_SEPARATOR; 114 *p = PATH_SEPARATOR;
115 } 115 }
116 if(access(opath, F_OK)) 116 if(access(opath, F_OK))
117#ifdef _WIN32 117#ifdef _WIN32
118 return mkdir(opath); 118 return mkdir(opath);
119#else 119#else
120 return mkdir(opath, S_IRWXU); 120 return mkdir(opath, S_IRWXU);
121#endif 121#endif
122 else 122 else
123 return -1; 123 return -1;
124} 124}
125 125
126#if 0 126#if 0
127static bool dir_exists(const char *dir) 127static bool dir_exists(const char *dir)
128{ 128{
129 struct stat buf; 129 struct stat buf;
130 memset(&buf, 0, sizeof(struct stat)); 130 memset(&buf, 0, sizeof(struct stat));
131 printf("start: %s\n", dir); 131 printf("start: %s\n", dir);
132 char *dir_cpy = (char*)malloc(strlen(dir)); 132 char *dir_cpy = (char*)malloc(strlen(dir));
133 strcpy(dir_cpy, dir); 133 strcpy(dir_cpy, dir);
134 printf("%s\n", dir_cpy); 134 printf("%s\n", dir_cpy);
135 int tmp = (int)dir_cpy; 135 int tmp = (int)dir_cpy;
136 while(*dir_cpy != 0) 136 while(*dir_cpy != 0)
137 { 137 {
138 dir_cpy++; 138 dir_cpy++;
139 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0) 139 if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
140 *dir_cpy = 0; 140 *dir_cpy = 0;
141 } 141 }
142 printf("while_done\n"); 142 printf("while_done\n");
143 dir_cpy = (char*)tmp; 143 dir_cpy = (char*)tmp;
144 printf("statting %s...\n", dir_cpy); 144 printf("statting %s...\n", dir_cpy);
145 tmp = stat(dir_cpy, &buf); 145 tmp = stat(dir_cpy, &buf);
146 printf("chk_dir(%s) = %d\n", dir_cpy, tmp); 146 printf("chk_dir(%s) = %d\n", dir_cpy, tmp);
147 free(dir_cpy); 147 free(dir_cpy);
148 return tmp == 0; 148 return tmp == 0;
149} 149}
150#endif 150#endif
151 151
152static bool file_exists(const char *file) 152static bool file_exists(const char *file)
153{ 153{
154 struct stat buf; 154 struct stat buf;
155 return stat(file, &buf) == 0; 155 return stat(file, &buf) == 0;
156} 156}
157 157
158 158
159static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath) 159static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
160{ 160{
161 FILE *outfile; 161 FILE *outfile;
162 char *filename; 162 char *filename;
163 unsigned int filenamesize, filesize; 163 unsigned int filenamesize, filesize;
164 while(size > 0) 164 while(size > 0)
165 { 165 {
166 filenamesize = le2int((unsigned char*)infile); 166 filenamesize = le2int((unsigned char*)infile);
167 infile += 4; 167 infile += 4;
168 size -= 4; 168 size -= 4;
169 if(size > 0) 169 if(size > 0)
170 { 170 {
171 filename = (char*)calloc(1, filenamesize+1+strlen(outpath)); 171 filename = (char*)calloc(1, filenamesize+1+strlen(outpath));
172 memcpy(filename, outpath, strlen(outpath)); 172 memcpy(filename, outpath, strlen(outpath));
173 memcpy(&filename[strlen(outpath)], infile, filenamesize); 173 memcpy(&filename[strlen(outpath)], infile, filenamesize);
174#ifndef _WIN32 174#ifndef _WIN32
175 replace(filename); 175 replace(filename);
176#endif 176#endif
177 infile += filenamesize + 1; /* + padding */ 177 infile += filenamesize + 1; /* + padding */
178 size -= filenamesize + 1; 178 size -= filenamesize + 1;
179 179
180 filesize = le2int((unsigned char*)infile); 180 filesize = le2int((unsigned char*)infile);
181 infile += 4; 181 infile += 4;
182 size -= 4; 182 size -= 4;
183#if 0 183#if 0
184 if(!dir_exists(basepath(filename))) 184 if(!dir_exists(basepath(filename)))
185#endif 185#endif
186 { 186 {
187 printf("[INFO] %s\n", basepath(filename)); 187 printf("[INFO] %s\n", basepath(filename));
188 if(__mkdir(basepath(filename)) != 0) 188 if(__mkdir(basepath(filename)) != 0)
189 { 189 {
190#if 0 190#if 0
191 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename)); 191 fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename));
192 return -3; 192 return -3;
193#endif 193#endif
194 } 194 }
195 } 195 }
196 196
197 if(!file_exists(filename)) 197 if(!file_exists(filename))
198 { 198 {
199 printf("[INFO] %s: %d bytes\n", filename, filesize); 199 printf("[INFO] %s: %d bytes\n", filename, filesize);
200 if((outfile = fopen(filename, "wb")) == NULL) 200 if((outfile = fopen(filename, "wb")) == NULL)
201 { 201 {
202 fprintf(stderr, "[ERR] Error opening file %s\n", filename); 202 fprintf(stderr, "[ERR] Error opening file %s\n", filename);
203 return -1; 203 return -1;
204 } 204 }
205 if(filesize>0) 205 if(filesize>0)
206 { 206 {
207 if(fwrite(infile, filesize, 1, outfile) != 1) 207 if(fwrite(infile, filesize, 1, outfile) != 1)
208 { 208 {
209 fclose(outfile); 209 fclose(outfile);
210 fprintf(stderr, "[ERR] Error writing to file %s\n", filename); 210 fprintf(stderr, "[ERR] Error writing to file %s\n", filename);
211 return -2; 211 return -2;
212 } 212 }
213 } 213 }
214 fclose(outfile); 214 fclose(outfile);
215 } 215 }
216 216
217 infile += filesize; 217 infile += filesize;
218 size -= filesize; 218 size -= filesize;
219 } 219 }
220 } 220 }
221 return 0; 221 return 0;
222} 222}
223 223
224static void print_usage(void) 224static void print_usage(void)
225{ 225{
226#ifdef _WIN32 226#ifdef _WIN32
227 fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n"); 227 fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
228 fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n"); 228 fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
229#else 229#else
230 fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n"); 230 fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
231 fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n"); 231 fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
232#endif 232#endif
233} 233}
234 234
235int main(int argc, char *argv[]) 235int main(int argc, char *argv[])
236{ 236{
237 FILE *infile; 237 FILE *infile;
238 struct header hdr; 238 struct header hdr;
239 unsigned char *inbuffer; 239 unsigned char *inbuffer;
240 240
241 fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); 241 fprintf(stderr, "HXFsplit v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
242 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n"); 242 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
243 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); 243 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
244 244
245 if(argc != 3) 245 if(argc != 3)
246 { 246 {
247 print_usage(); 247 print_usage();
248 return 1; 248 return 1;
249 } 249 }
250 250
251#ifdef _WIN32 251#ifdef _WIN32
252 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0) 252 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "\\") != 0)
253 { 253 {
254 fprintf(stderr, "[ERR] Output path must end with a \\\n"); 254 fprintf(stderr, "[ERR] Output path must end with a \\\n");
255#else 255#else
256 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0) 256 if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
257 { 257 {
258 fprintf(stderr, "[ERR] Output path must end with a /\n"); 258 fprintf(stderr, "[ERR] Output path must end with a /\n");
259#endif 259#endif
260 return 2; 260 return 2;
261 } 261 }
262 262
263 if((infile = fopen(argv[1], "rb")) == NULL) 263 if((infile = fopen(argv[1], "rb")) == NULL)
264 { 264 {
265 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); 265 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
266 return 3; 266 return 3;
267 } 267 }
268 268
269 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL) 269 if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
270 { 270 {
271 fclose(infile); 271 fclose(infile);
272 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header)); 272 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header));
273 return 4; 273 return 4;
274 } 274 }
275 275
276 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1) 276 if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
277 { 277 {
278 fclose(infile); 278 fclose(infile);
279 fprintf(stderr, "Cannot read header of %s\n", argv[1]); 279 fprintf(stderr, "Cannot read header of %s\n", argv[1]);
280 return 5; 280 return 5;
281 } 281 }
282 282
283 memcpy(hdr.main_header, inbuffer, 20); 283 memcpy(hdr.main_header, inbuffer, 20);
284 hdr.size = le2int(&inbuffer[20]); 284 hdr.size = le2int(&inbuffer[20]);
285 hdr.checksum = le2int(&inbuffer[24]); 285 hdr.checksum = le2int(&inbuffer[24]);
286 hdr.unknown = le2int(&inbuffer[28]); 286 hdr.unknown = le2int(&inbuffer[28]);
287 memcpy(hdr.other_header, &inbuffer[32], 32); 287 memcpy(hdr.other_header, &inbuffer[32], 32);
288 free(inbuffer); 288 free(inbuffer);
289 289
290 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0) 290 if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
291 { 291 {
292 fclose(infile); 292 fclose(infile);
293 fprintf(stderr, "[ERR] Header doesn't match\n"); 293 fprintf(stderr, "[ERR] Header doesn't match\n");
294 return 6; 294 return 6;
295 } 295 }
296 296
297 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL) 297 if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
298 { 298 {
299 fclose(infile); 299 fclose(infile);
300 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size); 300 fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size);
301 return 7; 301 return 7;
302 } 302 }
303 303
304 fseek(infile, sizeof(struct header), SEEK_SET); 304 fseek(infile, sizeof(struct header), SEEK_SET);
305 305
306 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1) 306 if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
307 { 307 {
308 fclose(infile); 308 fclose(infile);
309 free(inbuffer); 309 free(inbuffer);
310 fprintf(stderr, "[ERR] Cannot read file in buffer\n"); 310 fprintf(stderr, "[ERR] Cannot read file in buffer\n");
311 return 8; 311 return 8;
312 } 312 }
313 313
314 fclose(infile); 314 fclose(infile);
315 315
316 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]); 316 split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
317 317
318 free(inbuffer); 318 free(inbuffer);
319 319
320 return 0; 320 return 0;
321} 321}