diff options
Diffstat (limited to 'utils/jz4740_tools')
-rw-r--r-- | utils/jz4740_tools/HXFmerge.c | 642 | ||||
-rw-r--r-- | utils/jz4740_tools/HXFreplace.c | 484 | ||||
-rw-r--r-- | utils/jz4740_tools/HXFsplit.c | 642 |
3 files changed, 884 insertions, 884 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 @@ -/***************************************************************************
- * __________ __ ___.
- * 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;
-}
+/*************************************************************************** + * __________ __ ___. + * 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; +} diff --git a/utils/jz4740_tools/HXFreplace.c b/utils/jz4740_tools/HXFreplace.c index 989a59dd61..bcfff82540 100644 --- a/utils/jz4740_tools/HXFreplace.c +++ b/utils/jz4740_tools/HXFreplace.c @@ -1,242 +1,242 @@ -/***************************************************************************
- * __________ __ ___.
- * 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.1"
-
-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;
-}
-
-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;
-}
-
-static void print_usage(void)
-{
-#ifdef _WIN32
- fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
- fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
-#else
- fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
- fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\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 *infile, *outfile, *fw;
-
- fprintf(stderr, "HXFreplace 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 != 4)
- {
- print_usage();
- return 1;
- }
-
- if((infile = fopen(argv[1], "rb")) == NULL)
- {
- fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
- return 2;
- }
-
- if(fseek(infile, 0x40, SEEK_SET) != 0)
- {
- fprintf(stderr, "[ERR] Cannot seek to 0x40\n");
- fclose(infile);
- return 3;
- }
-
- fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n");
-
- int found = -1;
- int filenamesize;
- char *filename;
- unsigned char tmp[4];
-
-#define READ(x, len) if(fread(x, len, 1, infile) != 1) \
- { \
- fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
- fclose(infile); \
- return 4; \
- }
- while(found < 0)
- {
- READ(&tmp[0], 4);
- filenamesize = le2int(tmp);
- filename = (char*)malloc(filenamesize);
- READ(filename, filenamesize);
- if(strcmp(filename, "ccpmp.bin") == 0)
- found = ftell(infile);
- else
- {
- READ(&tmp[0], 4);
- fseek(infile, le2int(tmp), SEEK_CUR);
- }
- free(filename);
- }
-
- fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found);
-
- if((outfile = fopen(argv[2], "wb+")) == NULL)
- {
- fclose(infile);
- fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
- return 5;
- }
-
-#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
- { \
- fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
- fclose(outfile); \
- if(fw != NULL) \
- fclose(fw); \
- return 5; \
- }
-
- unsigned char* buffer;
-
- buffer = (unsigned char*)malloc(found);
- fseek(infile, 0, SEEK_SET);
- READ(buffer, found);
- WRITE(buffer, found);
- free(buffer);
-
- if((fw = fopen(argv[3], "rb")) == NULL)
- {
- fclose(infile);
- fclose(outfile);
- fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]);
- }
-
- int fw_filesize = _filesize(fw);
-
-#define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
- { \
- fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
- fclose(infile); \
- fclose(outfile); \
- return 6; \
- }
- buffer = (unsigned char*)malloc(fw_filesize);
- READ2(buffer, fw_filesize);
- fputc(0x20, outfile); /* Padding */
- WRITE(int2le(fw_filesize), 4);
- WRITE(buffer, fw_filesize);
- free(buffer);
- fclose(fw);
- fw = NULL;
-
- fseek(infile, found+1, SEEK_SET);
- READ(&tmp, 4);
- if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0)
- {
- fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]);
- fclose(infile);
- fclose(outfile);
- return 7;
- }
- found = ftell(infile);
-
- int other_size = _filesize(infile) - found;
- buffer = (unsigned char*)malloc(other_size);
- READ(buffer, other_size);
- WRITE(buffer, other_size);
- free(buffer);
- fclose(infile);
-
- fflush(outfile);
- fseek(outfile, 0x14, SEEK_SET);
- WRITE(int2le(_filesize(outfile)), 4);
- WRITE(int2le(checksum(outfile)), 4);
- fclose(outfile);
-
- fprintf(stderr, "[INFO] Done!\n");
-
- return 0;
-}
+/*************************************************************************** + * __________ __ ___. + * 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.1" + +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; +} + +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; +} + +static void print_usage(void) +{ +#ifdef _WIN32 + fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); + fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n"); +#else + fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); + fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\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 *infile, *outfile, *fw; + + fprintf(stderr, "HXFreplace 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 != 4) + { + print_usage(); + return 1; + } + + if((infile = fopen(argv[1], "rb")) == NULL) + { + fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); + return 2; + } + + if(fseek(infile, 0x40, SEEK_SET) != 0) + { + fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); + fclose(infile); + return 3; + } + + fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); + + int found = -1; + int filenamesize; + char *filename; + unsigned char tmp[4]; + +#define READ(x, len) if(fread(x, len, 1, infile) != 1) \ + { \ + fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ + fclose(infile); \ + return 4; \ + } + while(found < 0) + { + READ(&tmp[0], 4); + filenamesize = le2int(tmp); + filename = (char*)malloc(filenamesize); + READ(filename, filenamesize); + if(strcmp(filename, "ccpmp.bin") == 0) + found = ftell(infile); + else + { + READ(&tmp[0], 4); + fseek(infile, le2int(tmp), SEEK_CUR); + } + free(filename); + } + + fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); + + if((outfile = fopen(argv[2], "wb+")) == NULL) + { + fclose(infile); + fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); + return 5; + } + +#define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ + { \ + fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ + fclose(outfile); \ + if(fw != NULL) \ + fclose(fw); \ + return 5; \ + } + + unsigned char* buffer; + + buffer = (unsigned char*)malloc(found); + fseek(infile, 0, SEEK_SET); + READ(buffer, found); + WRITE(buffer, found); + free(buffer); + + if((fw = fopen(argv[3], "rb")) == NULL) + { + fclose(infile); + fclose(outfile); + fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); + } + + int fw_filesize = _filesize(fw); + +#define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ + { \ + fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ + fclose(infile); \ + fclose(outfile); \ + return 6; \ + } + buffer = (unsigned char*)malloc(fw_filesize); + READ2(buffer, fw_filesize); + fputc(0x20, outfile); /* Padding */ + WRITE(int2le(fw_filesize), 4); + WRITE(buffer, fw_filesize); + free(buffer); + fclose(fw); + fw = NULL; + + fseek(infile, found+1, SEEK_SET); + READ(&tmp, 4); + if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) + { + fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); + fclose(infile); + fclose(outfile); + return 7; + } + found = ftell(infile); + + int other_size = _filesize(infile) - found; + buffer = (unsigned char*)malloc(other_size); + READ(buffer, other_size); + WRITE(buffer, other_size); + free(buffer); + fclose(infile); + + fflush(outfile); + fseek(outfile, 0x14, SEEK_SET); + WRITE(int2le(_filesize(outfile)), 4); + WRITE(int2le(checksum(outfile)), 4); + fclose(outfile); + + fprintf(stderr, "[INFO] Done!\n"); + + return 0; +} 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 @@ -/***************************************************************************
- * __________ __ ___.
- * 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>
-
-#define VERSION "0.2"
-
-struct header{
- char main_header[20];
- unsigned int size;
- unsigned int checksum;
- unsigned int unknown;
- char other_header[32];
-};
-
-static char* basepath(char* path)
-{
- static char tmp[255];
- char *ptr, *ptr2, *ptr3;
- ptr = path;
- ptr2 = (char*)tmp;
-#ifdef _WIN32
- ptr3 = strrchr(path, 0x5C);
-#else
- ptr3 = strrchr(path, 0x2F);
-#endif
- while((int)ptr < (int)ptr3)
- {
- *ptr2 = *ptr;
- ptr++;
- ptr2++;
- }
-#ifdef _WIN32
- *ptr2 = 0x5C;
-#else
- *ptr2 = 0x2F;
-#endif
- ptr2++;
- *ptr2 = 0;
- return (char*)tmp;
-}
-
-#ifndef _WIN32
-static void replace(char* str)
-{
- char *ptr = str;
- while(*ptr != 0)
- {
- if(*ptr == 0x5C) /* \ */
- *ptr = 0x2F; /* / */
- ptr++;
- }
-}
-#endif
-
-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
-
-static unsigned int __mkdir(const char *path)
-{
- char opath[256];
- char *p;
- size_t len;
-
- strncpy(opath, path, sizeof(opath));
- len = strlen(opath);
- if(opath[len - 1] == PATH_SEPARATOR)
- opath[len - 1] = '\0';
- for(p = opath; *p; p++)
- if(*p == PATH_SEPARATOR)
- {
- *p = '\0';
- if(access(opath, F_OK))
-#ifdef _WIN32
- mkdir(opath);
-#else
- mkdir(opath, S_IRWXU);
-#endif
- *p = PATH_SEPARATOR;
- }
- if(access(opath, F_OK))
-#ifdef _WIN32
- return mkdir(opath);
-#else
- return mkdir(opath, S_IRWXU);
-#endif
- else
- return -1;
-}
-
-#if 0
-static bool dir_exists(const char *dir)
-{
- struct stat buf;
- memset(&buf, 0, sizeof(struct stat));
- printf("start: %s\n", dir);
- char *dir_cpy = (char*)malloc(strlen(dir));
- strcpy(dir_cpy, dir);
- printf("%s\n", dir_cpy);
- int tmp = (int)dir_cpy;
- while(*dir_cpy != 0)
- {
- dir_cpy++;
- if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0)
- *dir_cpy = 0;
- }
- printf("while_done\n");
- dir_cpy = (char*)tmp;
- printf("statting %s...\n", dir_cpy);
- tmp = stat(dir_cpy, &buf);
- printf("chk_dir(%s) = %d\n", dir_cpy, tmp);
- free(dir_cpy);
- return tmp == 0;
-}
-#endif
-
-static bool file_exists(const char *file)
-{
- struct stat buf;
- return stat(file, &buf) == 0;
-}
-
-
-static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath)
-{
- FILE *outfile;
- char *filename;
- unsigned int filenamesize, filesize;
- while(size > 0)
- {
- filenamesize = le2int((unsigned char*)infile);
- infile += 4;
- size -= 4;
- if(size > 0)
- {
- filename = (char*)calloc(1, filenamesize+1+strlen(outpath));
- memcpy(filename, outpath, strlen(outpath));
- memcpy(&filename[strlen(outpath)], infile, filenamesize);
-#ifndef _WIN32
- replace(filename);
-#endif
- infile += filenamesize + 1; /* + padding */
- size -= filenamesize + 1;
-
- filesize = le2int((unsigned char*)infile);
- infile += 4;
- size -= 4;
-#if 0
- if(!dir_exists(basepath(filename)))
-#endif
- {
- printf("[INFO] %s\n", basepath(filename));
- if(__mkdir(basepath(filename)) != 0)
- {
-#if 0
- fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename));
- return -3;
-#endif
- }
- }
-
- if(!file_exists(filename))
- {
- printf("[INFO] %s: %d bytes\n", filename, filesize);
- if((outfile = fopen(filename, "wb")) == NULL)
- {
- fprintf(stderr, "[ERR] Error opening file %s\n", filename);
- return -1;
- }
- if(filesize>0)
- {
- if(fwrite(infile, filesize, 1, outfile) != 1)
- {
- fclose(outfile);
- fprintf(stderr, "[ERR] Error writing to file %s\n", filename);
- return -2;
- }
- }
- fclose(outfile);
- }
-
- infile += filesize;
- size -= filesize;
- }
- }
- return 0;
-}
-
-static void print_usage(void)
-{
-#ifdef _WIN32
- fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n");
- fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n");
-#else
- fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n");
- fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n");
-#endif
-}
-
-int main(int argc, char *argv[])
-{
- FILE *infile;
- struct header hdr;
- unsigned char *inbuffer;
-
- fprintf(stderr, "HXFsplit 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[2]+strlen(argv[2])-1), "\\") != 0)
- {
- fprintf(stderr, "[ERR] Output path must end with a \\\n");
-#else
- if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0)
- {
- fprintf(stderr, "[ERR] Output path must end with a /\n");
-#endif
- return 2;
- }
-
- if((infile = fopen(argv[1], "rb")) == NULL)
- {
- fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
- return 3;
- }
-
- if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL)
- {
- fclose(infile);
- fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header));
- return 4;
- }
-
- if(fread(inbuffer, sizeof(struct header), 1, infile) != 1)
- {
- fclose(infile);
- fprintf(stderr, "Cannot read header of %s\n", argv[1]);
- return 5;
- }
-
- memcpy(hdr.main_header, inbuffer, 20);
- hdr.size = le2int(&inbuffer[20]);
- hdr.checksum = le2int(&inbuffer[24]);
- hdr.unknown = le2int(&inbuffer[28]);
- memcpy(hdr.other_header, &inbuffer[32], 32);
- free(inbuffer);
-
- if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0)
- {
- fclose(infile);
- fprintf(stderr, "[ERR] Header doesn't match\n");
- return 6;
- }
-
- if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL)
- {
- fclose(infile);
- fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size);
- return 7;
- }
-
- fseek(infile, sizeof(struct header), SEEK_SET);
-
- if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1)
- {
- fclose(infile);
- free(inbuffer);
- fprintf(stderr, "[ERR] Cannot read file in buffer\n");
- return 8;
- }
-
- fclose(infile);
-
- split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]);
-
- free(inbuffer);
-
- return 0;
-}
+/*************************************************************************** + * __________ __ ___. + * 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> + +#define VERSION "0.2" + +struct header{ + char main_header[20]; + unsigned int size; + unsigned int checksum; + unsigned int unknown; + char other_header[32]; +}; + +static char* basepath(char* path) +{ + static char tmp[255]; + char *ptr, *ptr2, *ptr3; + ptr = path; + ptr2 = (char*)tmp; +#ifdef _WIN32 + ptr3 = strrchr(path, 0x5C); +#else + ptr3 = strrchr(path, 0x2F); +#endif + while((int)ptr < (int)ptr3) + { + *ptr2 = *ptr; + ptr++; + ptr2++; + } +#ifdef _WIN32 + *ptr2 = 0x5C; +#else + *ptr2 = 0x2F; +#endif + ptr2++; + *ptr2 = 0; + return (char*)tmp; +} + +#ifndef _WIN32 +static void replace(char* str) +{ + char *ptr = str; + while(*ptr != 0) + { + if(*ptr == 0x5C) /* \ */ + *ptr = 0x2F; /* / */ + ptr++; + } +} +#endif + +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 + +static unsigned int __mkdir(const char *path) +{ + char opath[256]; + char *p; + size_t len; + + strncpy(opath, path, sizeof(opath)); + len = strlen(opath); + if(opath[len - 1] == PATH_SEPARATOR) + opath[len - 1] = '\0'; + for(p = opath; *p; p++) + if(*p == PATH_SEPARATOR) + { + *p = '\0'; + if(access(opath, F_OK)) +#ifdef _WIN32 + mkdir(opath); +#else + mkdir(opath, S_IRWXU); +#endif + *p = PATH_SEPARATOR; + } + if(access(opath, F_OK)) +#ifdef _WIN32 + return mkdir(opath); +#else + return mkdir(opath, S_IRWXU); +#endif + else + return -1; +} + +#if 0 +static bool dir_exists(const char *dir) +{ + struct stat buf; + memset(&buf, 0, sizeof(struct stat)); + printf("start: %s\n", dir); + char *dir_cpy = (char*)malloc(strlen(dir)); + strcpy(dir_cpy, dir); + printf("%s\n", dir_cpy); + int tmp = (int)dir_cpy; + while(*dir_cpy != 0) + { + dir_cpy++; + if(*dir_cpy == PATH_SEPARATOR && *(dir_cpy+1) == 0) + *dir_cpy = 0; + } + printf("while_done\n"); + dir_cpy = (char*)tmp; + printf("statting %s...\n", dir_cpy); + tmp = stat(dir_cpy, &buf); + printf("chk_dir(%s) = %d\n", dir_cpy, tmp); + free(dir_cpy); + return tmp == 0; +} +#endif + +static bool file_exists(const char *file) +{ + struct stat buf; + return stat(file, &buf) == 0; +} + + +static int split_hxf(const unsigned char* infile, unsigned int size, const char* outpath) +{ + FILE *outfile; + char *filename; + unsigned int filenamesize, filesize; + while(size > 0) + { + filenamesize = le2int((unsigned char*)infile); + infile += 4; + size -= 4; + if(size > 0) + { + filename = (char*)calloc(1, filenamesize+1+strlen(outpath)); + memcpy(filename, outpath, strlen(outpath)); + memcpy(&filename[strlen(outpath)], infile, filenamesize); +#ifndef _WIN32 + replace(filename); +#endif + infile += filenamesize + 1; /* + padding */ + size -= filenamesize + 1; + + filesize = le2int((unsigned char*)infile); + infile += 4; + size -= 4; +#if 0 + if(!dir_exists(basepath(filename))) +#endif + { + printf("[INFO] %s\n", basepath(filename)); + if(__mkdir(basepath(filename)) != 0) + { +#if 0 + fprintf(stderr, "[ERR] Error creating directory %s\n", basepath(filename)); + return -3; +#endif + } + } + + if(!file_exists(filename)) + { + printf("[INFO] %s: %d bytes\n", filename, filesize); + if((outfile = fopen(filename, "wb")) == NULL) + { + fprintf(stderr, "[ERR] Error opening file %s\n", filename); + return -1; + } + if(filesize>0) + { + if(fwrite(infile, filesize, 1, outfile) != 1) + { + fclose(outfile); + fprintf(stderr, "[ERR] Error writing to file %s\n", filename); + return -2; + } + } + fclose(outfile); + } + + infile += filesize; + size -= filesize; + } + } + return 0; +} + +static void print_usage(void) +{ +#ifdef _WIN32 + fprintf(stderr, "Usage: hxfsplit.exe [FW] [OUTPUT_DIR]\n\n"); + fprintf(stderr, "Example: hxfsplit.exe VX747.HXF VX747_extracted\\\n\n"); +#else + fprintf(stderr, "Usage: HXFsplit [FW] [OUTPUT_DIR]\n\n"); + fprintf(stderr, "Example: HXFsplit VX747.HXF VX747_extracted/\n\n"); +#endif +} + +int main(int argc, char *argv[]) +{ + FILE *infile; + struct header hdr; + unsigned char *inbuffer; + + fprintf(stderr, "HXFsplit 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[2]+strlen(argv[2])-1), "\\") != 0) + { + fprintf(stderr, "[ERR] Output path must end with a \\\n"); +#else + if(strcmp((char*)(argv[2]+strlen(argv[2])-1), "/") != 0) + { + fprintf(stderr, "[ERR] Output path must end with a /\n"); +#endif + return 2; + } + + if((infile = fopen(argv[1], "rb")) == NULL) + { + fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); + return 3; + } + + if((inbuffer = (unsigned char*)malloc(sizeof(struct header))) == NULL) + { + fclose(infile); + fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", sizeof(struct header)); + return 4; + } + + if(fread(inbuffer, sizeof(struct header), 1, infile) != 1) + { + fclose(infile); + fprintf(stderr, "Cannot read header of %s\n", argv[1]); + return 5; + } + + memcpy(hdr.main_header, inbuffer, 20); + hdr.size = le2int(&inbuffer[20]); + hdr.checksum = le2int(&inbuffer[24]); + hdr.unknown = le2int(&inbuffer[28]); + memcpy(hdr.other_header, &inbuffer[32], 32); + free(inbuffer); + + if(strcmp(hdr.other_header, "Chinachip PMP firmware V1.0") != 0) + { + fclose(infile); + fprintf(stderr, "[ERR] Header doesn't match\n"); + return 6; + } + + if((inbuffer = (unsigned char*)malloc(hdr.size)) == NULL) + { + fclose(infile); + fprintf(stderr, "[ERR] Error allocating %d bytes buffer\n", hdr.size); + return 7; + } + + fseek(infile, sizeof(struct header), SEEK_SET); + + if(fread(inbuffer, hdr.size-sizeof(struct header), 1, infile) != 1) + { + fclose(infile); + free(inbuffer); + fprintf(stderr, "[ERR] Cannot read file in buffer\n"); + return 8; + } + + fclose(infile); + + split_hxf(inbuffer, hdr.size-sizeof(struct header), argv[2]); + + free(inbuffer); + + return 0; +} |