summaryrefslogtreecommitdiff
path: root/utils/jz4740_tools/HXFmerge.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/jz4740_tools/HXFmerge.c')
-rw-r--r--utils/jz4740_tools/HXFmerge.c642
1 files changed, 321 insertions, 321 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}