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