summaryrefslogtreecommitdiff
path: root/utils/jz4740_tools/HXFmerge.c
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-06-28 16:49:46 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-06-28 16:49:46 +0000
commitd951e169f5f4e82590b36b1e703a09e71da1fdd1 (patch)
tree45480d4388cc307c97cfba0edd31a2f07e4c34f6 /utils/jz4740_tools/HXFmerge.c
parent9c84070c8a56b89b32c92eeca06154ac6ad927a5 (diff)
downloadrockbox-d951e169f5f4e82590b36b1e703a09e71da1fdd1.tar.gz
rockbox-d951e169f5f4e82590b36b1e703a09e71da1fdd1.zip
Rename jz4740 tools + add some new ones
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17840 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/jz4740_tools/HXFmerge.c')
-rwxr-xr-xutils/jz4740_tools/HXFmerge.c291
1 files changed, 291 insertions, 0 deletions
diff --git a/utils/jz4740_tools/HXFmerge.c b/utils/jz4740_tools/HXFmerge.c
new file mode 100755
index 0000000000..40c36e7bc7
--- /dev/null
+++ b/utils/jz4740_tools/HXFmerge.c
@@ -0,0 +1,291 @@
1/*
2Made by Maurus Cuelenaere
3*/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/types.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <sys/stat.h>
12#include <stdbool.h>
13#include <dirent.h>
14
15#define VERSION "0.2"
16
17static unsigned char* int2le(unsigned int val)
18{
19 static unsigned char addr[4];
20 addr[0] = val & 0xff;
21 addr[1] = (val >> 8) & 0xff;
22 addr[2] = (val >> 16) & 0xff;
23 addr[3] = (val >> 24) & 0xff;
24 return addr;
25}
26
27static unsigned int le2int(unsigned char* buf)
28{
29 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
30
31 return res;
32}
33
34#ifdef _WIN32
35 #define PATH_SEPARATOR "\\"
36#else
37 #define PATH_SEPARATOR "/"
38#endif
39
40#ifndef _WIN32
41
42#define MIN(a, b) (a > b ? b : a)
43static char* replace(char* str)
44{
45 char tmp[255];
46 memcpy(tmp, str, MIN(strlen(str), 255);
47 char *ptr = tmp;
48 while(*ptr != 0)
49 {
50 if(*ptr == 0x2F) /* /*/
51 *ptr = 0x5C; /* \ */
52 ptr++;
53 }
54 return tmp;
55}
56#endif
57
58static bool is_dir(const char* name1, const char* name2)
59{
60 char *name;
61 DIR *directory;
62 name = (char*)malloc(strlen(name1)+strlen(name2)+1);
63 strcpy(name, name1);
64 strcat(name, name2);
65 directory = opendir(name);
66 free(name);
67 if(directory)
68 {
69 closedir(directory);
70 return true;
71 }
72 else
73 return false;
74}
75
76unsigned int _filesize(FILE* fd)
77{
78 unsigned int tmp, oldpos;
79 oldpos = ftell(fd);
80 fseek(fd, 0, SEEK_END);
81 tmp = ftell(fd);
82 fseek(fd, oldpos, SEEK_SET);
83 return tmp;
84}
85#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
86 { \
87 closedir(indir_handle); \
88 if(filesize > 0) \
89 free(buffer); \
90 fprintf(stderr, "[ERR] Error writing to file\n"); \
91 return; \
92 }
93static void merge_hxf(const char* indir, FILE* outfile, const char* add)
94{
95 DIR *indir_handle;
96 struct dirent *dirs;
97 char dir[255];
98 strcpy(dir, indir);
99 strcat(dir, add);
100
101 if((indir_handle = opendir(dir)) == NULL)
102 {
103 fprintf(stderr, "[ERR] Error opening dir %s\n", indir);
104 return;
105 }
106
107 while((dirs = readdir(indir_handle)) != NULL)
108 {
109 if(strcmp(dirs->d_name, "..") != 0 &&
110 strcmp(dirs->d_name, ".") != 0)
111 {
112 fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name);
113 if(is_dir(dir, dirs->d_name))
114 {
115 char dir2[255];
116 strcpy(dir2, add);
117 strcat(dir2, dirs->d_name);
118 strcat(dir2, PATH_SEPARATOR);
119 merge_hxf(indir, outfile, dir2);
120 }
121 else
122 {
123 FILE *filehandle;
124 unsigned char *buffer;
125 char file[255];
126 unsigned int filesize;
127 strcpy(file, dir);
128 strcat(file, dirs->d_name);
129 if((filehandle = fopen(file, "rb")) == NULL)
130 {
131 fprintf(stderr, "[ERR] Cannot open %s\n", file);
132 closedir(indir_handle);
133 return;
134 }
135 filesize = _filesize(filehandle);
136 if(filesize > 0)
137 {
138 buffer = (unsigned char*)malloc(filesize);
139 if(buffer == NULL)
140 {
141 fclose(filehandle);
142 closedir(indir_handle);
143 fprintf(stderr, "[ERR] Cannot allocate memory\n");
144 return;
145 }
146 if(fread(buffer, filesize, 1, filehandle) != 1)
147 {
148 fclose(filehandle);
149 closedir(indir_handle);
150 free(buffer);
151 fprintf(stderr, "[ERR] Cannot read from %s%s%s\n", add, PATH_SEPARATOR, dirs->d_name);
152 return;
153 }
154 }
155 fclose(filehandle);
156
157 if(strlen(add)>0)
158 {
159 WRITE(int2le(dirs->d_namlen+strlen(add)), 4);
160#ifndef _WIN32
161 WRITE(replace(add), strlen(add)-1);
162#else
163 WRITE(add, strlen(add)-1);
164#endif
165 WRITE(PATH_SEPARATOR, 1);
166 WRITE(dirs->d_name, dirs->d_namlen);
167 }
168 else
169 {
170 WRITE(int2le(dirs->d_namlen), 4);
171 WRITE(dirs->d_name, dirs->d_namlen);
172 }
173 WRITE(int2le(filesize), 4);
174 if(filesize>0)
175 {
176 WRITE(buffer, filesize);
177 free(buffer);
178 }
179 }
180 }
181 }
182 closedir(indir_handle);
183}
184
185static void print_usage(void)
186{
187#ifdef _WIN32
188 fprintf(stderr, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n");
189 fprintf(stderr, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n");
190#else
191 fprintf(stderr, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n");
192 fprintf(stderr, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n");
193#endif
194}
195
196static unsigned int checksum(FILE *file)
197{
198 int oldpos = ftell(file);
199 unsigned int ret, i, filesize = _filesize(file)-0x40;
200 unsigned char *buf;
201
202 buf = (unsigned char*)malloc(filesize);
203
204 if(buf == NULL)
205 {
206 fseek(file, oldpos, SEEK_SET);
207 fprintf(stderr, "[ERR] Error while allocating memory\n");
208 return 0;
209 }
210
211 fseek(file, 0x40, SEEK_SET);
212 if(fread(buf, filesize, 1, file) != 1)
213 {
214 free(buf);
215 fseek(file, oldpos, SEEK_SET);
216 fprintf(stderr, "[ERR] Error while reading from file\n");
217 return 0;
218 }
219
220 fprintf(stderr, "[INFO] Computing checksum...");
221
222 for(i = 0; i < filesize; i+=4)
223 ret += le2int(&buf[i]);
224
225 free(buf);
226 fseek(file, oldpos, SEEK_SET);
227
228 fprintf(stderr, " Done!\n");
229 return ret;
230}
231
232int main(int argc, char *argv[])
233{
234 FILE *outfile;
235
236 fprintf(stderr, "HXFmerge v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
237 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
238 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
239
240 if(argc != 3)
241 {
242 print_usage();
243 return 1;
244 }
245
246#ifdef _WIN32
247 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0)
248 {
249 fprintf(stderr, "[ERR] Input path must end with a \\\n");
250#else
251 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0)
252 {
253 fprintf(stderr, "[ERR] Input path must end with a /\n");
254#endif
255 return 2;
256 }
257
258 if((outfile = fopen(argv[2], "wb+")) == NULL)
259 {
260 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
261 return 3;
262 }
263
264 fseek(outfile, 0x40, SEEK_SET);
265
266 merge_hxf(argv[1], outfile, "");
267
268 fflush(outfile);
269
270 fprintf(stderr, "[INFO] Filling header...\n");
271
272#undef WRITE
273#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
274 { \
275 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \
276 fclose(outfile); \
277 return 4; \
278 }
279 fflush(outfile);
280 fseek(outfile, 0, SEEK_SET);
281 WRITE("WADF0100200804111437", 20);
282 WRITE(int2le(_filesize(outfile)), 4);
283 WRITE(int2le(checksum(outfile)), 4);
284 WRITE(int2le(0), 4);
285 WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32);
286 fclose(outfile);
287
288 fprintf(stderr, "[INFO] Done!\n");
289
290 return 0;
291}