summaryrefslogtreecommitdiffstats
path: root/utils/jz4740_tools/HXFmerge.c
blob: 6b6c62505e13ed1519f72381c4b7b030076dbe57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Maurus Cuelenaere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <dirent.h>

#define VERSION "0.2"

static unsigned char* int2le(unsigned int val)
{
    static unsigned char addr[4];
    addr[0] = val & 0xff;
    addr[1] = (val >> 8) & 0xff;
    addr[2] = (val >> 16) & 0xff;
    addr[3] = (val >> 24) & 0xff;
    return addr;
}

static unsigned int le2int(unsigned char* buf)
{
   unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];

   return res;
}

#ifdef _WIN32
 #define PATH_SEPARATOR "\\"
#else
 #define PATH_SEPARATOR "/"
#endif

#ifndef _WIN32

#define MIN(a, b) (a > b ? b : a)
static char* replace(char* str)
{
    static char tmp[255];
    memcpy(tmp, str, MIN(strlen(str), 255));
    char *ptr = tmp;
    while(*ptr != 0)
    {
        if(*ptr == 0x2F) /* /*/
            *ptr = 0x5C; /* \ */
        ptr++;
    }
    return tmp;
}
#endif

static bool is_dir(const char* name1, const char* name2)
{
    char *name;
    DIR *directory;
    name = (char*)malloc(strlen(name1)+strlen(name2)+1);
    strcpy(name, name1);
    strcat(name, name2);
    directory = opendir(name);
    free(name);
    if(directory)
    {
        closedir(directory);
        return true;
    }
    else
        return false;
}

unsigned int _filesize(FILE* fd)
{
    unsigned int tmp, oldpos;
    oldpos = ftell(fd);
    fseek(fd, 0, SEEK_END);
    tmp = ftell(fd);
    fseek(fd, oldpos, SEEK_SET);
    return tmp;
}
#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
                      { \
                          closedir(indir_handle); \
                          if(filesize > 0) \
                            free(buffer); \
                          fprintf(stderr, "[ERR]  Error writing to file\n"); \
                          return; \
                      }
static void merge_hxf(const char* indir, FILE* outfile, const char* add)
{
    DIR *indir_handle;
    struct dirent *dirs;
    char dir[255];
    strcpy(dir, indir);
    strcat(dir, add);
    
    if((indir_handle = opendir(dir)) == NULL)
    {
        fprintf(stderr, "[ERR]  Error opening dir %s\n", indir);
        return;
    }
    
    while((dirs = readdir(indir_handle)) != NULL)
    {
        if(strcmp(dirs->d_name, "..") != 0 &&
           strcmp(dirs->d_name, ".") != 0)
        {
            fprintf(stderr, "[INFO] %s\%s\n", add, dirs->d_name);
            if(is_dir(dir, dirs->d_name))
            {
                char dir2[255];
                strcpy(dir2, add);
                strcat(dir2, dirs->d_name);
                strcat(dir2, PATH_SEPARATOR);
                merge_hxf(indir, outfile, dir2);
            }
            else
            {
                FILE *filehandle;
                unsigned char *buffer;
                char file[255];
                unsigned int filesize;
                strcpy(file, dir);
                strcat(file, dirs->d_name);
                if((filehandle = fopen(file, "rb")) == NULL)
                {
                    fprintf(stderr, "[ERR]  Cannot open %s\n", file);
                    closedir(indir_handle);
                    return;
                }
                filesize = _filesize(filehandle);
                if(filesize > 0)
                {
                    buffer = (unsigned char*)malloc(filesize);
                    if(buffer == NULL)
                    {
                        fclose(filehandle);
                        closedir(indir_handle);
                        fprintf(stderr, "[ERR]  Cannot allocate memory\n");
                        return;
                    }
                    if(fread(buffer, filesize, 1, filehandle) != 1)
                    {
                        fclose(filehandle);
                        closedir(indir_handle);
                        free(buffer);
                        fprintf(stderr, "[ERR]  Cannot read from %s%s%s\n", add, PATH_SEPARATOR, dirs->d_name);
                        return;
                    }
                }
                fclose(filehandle);
                
                if(strlen(add)>0)
                {
#ifdef _DIRENT_HAVE_D_NAMLEN
                    WRITE(int2le(dirs->d_namlen+strlen(add)), 4);
#else
                    WRITE(int2le(strlen(dirs->d_name)+strlen(add)), 4);
#endif
#ifndef _WIN32
                    WRITE(replace((char*)add), strlen(add)-1);
#else
                    WRITE(add, strlen(add)-1);
#endif
                    WRITE(PATH_SEPARATOR, 1);
#ifdef _DIRENT_HAVE_D_NAMLEN
                    WRITE(dirs->d_name, dirs->d_namlen);
#else
                    WRITE(dirs->d_name, strlen(dirs->d_name));
#endif
                }
                else
                {
#ifdef _DIRENT_HAVE_D_NAMLEN
                    WRITE(int2le(dirs->d_namlen), 4);
                    WRITE(dirs->d_name, dirs->d_namlen);
#else
                    WRITE(int2le(strlen(dirs->d_name)), 4);
                    WRITE(dirs->d_name, strlen(dirs->d_name));
#endif
                }
                WRITE(int2le(filesize), 4);
                if(filesize>0)
                {
                    WRITE(buffer, filesize);
                    free(buffer);
                }
            }
        }
    }
    closedir(indir_handle);
}

static void print_usage(void)
{
#ifdef _WIN32
    fprintf(stderr, "Usage: hxfmerge.exe [INPUT_DIR] [FW]\n\n");
    fprintf(stderr, "Example: hxfmerge.exe VX747_extracted\\ VX747.HXF\n\n");
#else
    fprintf(stderr, "Usage: HXFmerge [INPUT_DIR] [FW]\n\n");
    fprintf(stderr, "Example: HXFmerge VX747_extracted/ VX747.HXF\n\n");
#endif
}

static int checksum(FILE *file)
{
    int oldpos = ftell(file);
    int ret=0, i, filesize = _filesize(file)-0x40;
    unsigned char *buf;
    
    buf = (unsigned char*)malloc(filesize);
    
    if(buf == NULL)
    {
        fseek(file, oldpos, SEEK_SET);
        fprintf(stderr, "[ERR] Error while allocating memory\n");
        return 0;
    }
    
    fseek(file, 0x40, SEEK_SET);
    if(fread(buf, filesize, 1, file) != 1)
    {
        free(buf);
        fseek(file, oldpos, SEEK_SET);
        fprintf(stderr, "[ERR] Error while reading from file\n");
        return 0;
    }
    
    fprintf(stderr, "[INFO] Computing checksum...");
    
    for(i = 0; i < filesize; i+=4)
        ret += le2int(&buf[i]);
    
    free(buf);
    fseek(file, oldpos, SEEK_SET);
    
    fprintf(stderr, " Done!\n");
    return ret;
}

int main(int argc, char *argv[])
{
    FILE *outfile;
    
    fprintf(stderr, "HXFmerge v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
    fprintf(stderr, "This is free software; see the source for copying conditions.  There is NO\n");
    fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
    
    if(argc != 3)
    {
        print_usage();
        return 1;
    }

#ifdef _WIN32
    if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "\\") != 0)
    {
        fprintf(stderr, "[ERR]  Input path must end with a \\\n");
#else
    if(strcmp((char*)(argv[1]+strlen(argv[1])-1), "/") != 0)
    {
        fprintf(stderr, "[ERR]  Input path must end with a /\n");
#endif
        return 2;
    }

    if((outfile = fopen(argv[2], "wb+")) == NULL)
    {
        fprintf(stderr, "[ERR]  Cannot open %s\n", argv[2]);
        return 3;
    }
    
    fseek(outfile, 0x40, SEEK_SET);
    
    merge_hxf(argv[1], outfile, "");
    
    fflush(outfile);
    
    fprintf(stderr, "[INFO] Filling header...\n");
    
#undef WRITE
#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
                      { \
                            fprintf(stderr, "[ERR]  Cannot write to %s\n", argv[1]); \
                            fclose(outfile); \
                            return 4; \
                      }
    fflush(outfile);
    fseek(outfile, 0, SEEK_SET);
    WRITE("WADF0100200804111437", 20);
    WRITE(int2le(_filesize(outfile)), 4);
    WRITE(int2le(checksum(outfile)), 4);
    WRITE(int2le(0), 4);
    WRITE("Chinachip PMP firmware V1.0\0\0\0\0\0", 32);
    fclose(outfile);
    
    fprintf(stderr, "[INFO] Done!\n");
    
    return 0;
}