Fix checksum issues
[Rockbox.git] / utils / jz4740_tools / HXFmerge.c
blobf90d094fd9ad68a229302d98870dc7a00b75fa0f
1 /*
2 Made by Maurus Cuelenaere
3 */
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>
15 #define VERSION "0.2"
17 static unsigned char* int2le(unsigned int val)
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;
27 static unsigned int le2int(unsigned char* buf)
29 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
31 return res;
34 #ifdef _WIN32
35 #define PATH_SEPARATOR "\\"
36 #else
37 #define PATH_SEPARATOR "/"
38 #endif
40 #ifndef _WIN32
42 #define MIN(a, b) (a > b ? b : a)
43 static char* replace(char* str)
45 char tmp[255];
46 memcpy(tmp, str, MIN(strlen(str), 255);
47 char *ptr = tmp;
48 while(*ptr != 0)
50 if(*ptr == 0x2F) /* /*/
51 *ptr = 0x5C; /* \ */
52 ptr++;
54 return tmp;
56 #endif
58 static bool is_dir(const char* name1, const char* name2)
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)
69 closedir(directory);
70 return true;
72 else
73 return false;
76 unsigned int _filesize(FILE* fd)
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;
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; \
93 static void merge_hxf(const char* indir, FILE* outfile, const char* add)
95 DIR *indir_handle;
96 struct dirent *dirs;
97 char dir[255];
98 strcpy(dir, indir);
99 strcat(dir, add);
101 if((indir_handle = opendir(dir)) == NULL)
103 fprintf(stderr, "[ERR] Error opening dir %s\n", indir);
104 return;
107 while((dirs = readdir(indir_handle)) != NULL)
109 if(strcmp(dirs->d_name, "..") != 0 &&
110 strcmp(dirs->d_name, ".") != 0)
112 fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name);
113 if(is_dir(dir, dirs->d_name))
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);
121 else
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)
131 fprintf(stderr, "[ERR] Cannot open %s\n", file);
132 closedir(indir_handle);
133 return;
135 filesize = _filesize(filehandle);
136 if(filesize > 0)
138 buffer = (unsigned char*)malloc(filesize);
139 if(buffer == NULL)
141 fclose(filehandle);
142 closedir(indir_handle);
143 fprintf(stderr, "[ERR] Cannot allocate memory\n");
144 return;
146 if(fread(buffer, filesize, 1, filehandle) != 1)
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;
155 fclose(filehandle);
157 if(strlen(add)>0)
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);
168 else
170 WRITE(int2le(dirs->d_namlen), 4);
171 WRITE(dirs->d_name, dirs->d_namlen);
173 WRITE(int2le(filesize), 4);
174 if(filesize>0)
176 WRITE(buffer, filesize);
177 free(buffer);
182 closedir(indir_handle);
185 static void print_usage(void)
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
196 static int checksum(FILE *file)
198 int oldpos = ftell(file);
199 int ret=0, i, filesize = _filesize(file)-0x40;
200 unsigned char *buf;
202 buf = (unsigned char*)malloc(filesize);
204 if(buf == NULL)
206 fseek(file, oldpos, SEEK_SET);
207 fprintf(stderr, "[ERR] Error while allocating memory\n");
208 return 0;
211 fseek(file, 0x40, SEEK_SET);
212 if(fread(buf, filesize, 1, file) != 1)
214 free(buf);
215 fseek(file, oldpos, SEEK_SET);
216 fprintf(stderr, "[ERR] Error while reading from file\n");
217 return 0;
220 fprintf(stderr, "[INFO] Computing checksum...");
222 for(i = 0; i < filesize; i+=4)
223 ret += le2int(&buf[i]);
225 free(buf);
226 fseek(file, oldpos, SEEK_SET);
228 fprintf(stderr, " Done!\n");
229 return ret;
232 int main(int argc, char *argv[])
234 FILE *outfile;
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");
240 if(argc != 3)
242 print_usage();
243 return 1;
246 #ifdef _WIN32
247 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0)
249 fprintf(stderr, "[ERR] Input path must end with a \\\n");
250 #else
251 if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0)
253 fprintf(stderr, "[ERR] Input path must end with a /\n");
254 #endif
255 return 2;
258 if((outfile = fopen(argv[2], "wb+")) == NULL)
260 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
261 return 3;
264 fseek(outfile, 0x40, SEEK_SET);
266 merge_hxf(argv[1], outfile, "");
268 fflush(outfile);
270 fprintf(stderr, "[INFO] Filling header...\n");
272 #undef WRITE
273 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
275 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[1]); \
276 fclose(outfile); \
277 return 4; \
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);
288 fprintf(stderr, "[INFO] Done!\n");
290 return 0;