forked from len0rd/rockbox
Rename jz4740 tools + add some new ones
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17840 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
9c84070c8a
commit
d951e169f5
13 changed files with 820 additions and 0 deletions
291
utils/jz4740_tools/HXFmerge.c
Executable file
291
utils/jz4740_tools/HXFmerge.c
Executable file
|
|
@ -0,0 +1,291 @@
|
|||
/*
|
||||
Made by Maurus Cuelenaere
|
||||
*/
|
||||
|
||||
#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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
WRITE(int2le(dirs->d_namlen+strlen(add)), 4);
|
||||
#ifndef _WIN32
|
||||
WRITE(replace(add), strlen(add)-1);
|
||||
#else
|
||||
WRITE(add, strlen(add)-1);
|
||||
#endif
|
||||
WRITE(PATH_SEPARATOR, 1);
|
||||
WRITE(dirs->d_name, dirs->d_namlen);
|
||||
}
|
||||
else
|
||||
{
|
||||
WRITE(int2le(dirs->d_namlen), 4);
|
||||
WRITE(dirs->d_name, dirs->d_namlen);
|
||||
}
|
||||
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 unsigned int checksum(FILE *file)
|
||||
{
|
||||
int oldpos = ftell(file);
|
||||
unsigned int ret, 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;
|
||||
}
|
||||
225
utils/jz4740_tools/HXFreplace.c
Executable file
225
utils/jz4740_tools/HXFreplace.c
Executable file
|
|
@ -0,0 +1,225 @@
|
|||
/*
|
||||
Made by Maurus Cuelenaere
|
||||
*/
|
||||
|
||||
#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 unsigned int checksum(FILE *file)
|
||||
{
|
||||
int oldpos = ftell(file);
|
||||
unsigned int ret, 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;
|
||||
}
|
||||
304
utils/jz4740_tools/HXFsplit.c
Executable file
304
utils/jz4740_tools/HXFsplit.c
Executable file
|
|
@ -0,0 +1,304 @@
|
|||
/*
|
||||
Made by Maurus Cuelenaere
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
9
utils/jz4740_tools/Makefile
Executable file
9
utils/jz4740_tools/Makefile
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
WIN_DRIVERS_LIBUSB_DIR = D:\Program Files\LibUSB-Win32
|
||||
WIN_LIBUSB_INCLUDE_DIR = "$(WIN_DRIVERS_LIBUSB_DIR)\include"
|
||||
WIN_LIBUSB_LIB_DIR = "$(WIN_DRIVERS_LIBUSB_DIR)\lib\gcc"
|
||||
|
||||
usbtool:
|
||||
gcc -Wall -o usbtool jz4740_usbtool.c -lusb
|
||||
|
||||
usbtoolwin:
|
||||
gcc -Wall -o usbtool.exe jz4740_usbtool.c -lusb -I $(WIN_LIBUSB_INCLUDE_DIR) -L $(WIN_LIBUSB_LIB_DIR)
|
||||
2358
utils/jz4740_tools/jz4740.h
Executable file
2358
utils/jz4740_tools/jz4740.h
Executable file
File diff suppressed because it is too large
Load diff
732
utils/jz4740_tools/jz4740_usbtool.c
Executable file
732
utils/jz4740_tools/jz4740_usbtool.c
Executable file
|
|
@ -0,0 +1,732 @@
|
|||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
* $Id$
|
||||
*
|
||||
* Copyright (C) 2008 by Maurus Cuelenaere
|
||||
*
|
||||
* based on tcctool.c by Dave Chapman
|
||||
*
|
||||
* USB code based on ifp-line - http://ifp-driver.sourceforge.net
|
||||
*
|
||||
* ifp-line is (C) Pavel Kriz, Jun Yamishiro and Joe Roback and
|
||||
* licensed under the GPL (v2)
|
||||
*
|
||||
*
|
||||
* All files in this archive are subject to the GNU General Public License.
|
||||
* See the file COPYING in the source tree root for full license agreement.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <usb.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "jz4740.h"
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define VERSION "0.3"
|
||||
|
||||
#define MAX_FIRMWARESIZE (64*1024*1024) /* Arbitrary limit (for safety) */
|
||||
|
||||
/* For win32 compatibility: */
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
/* USB IDs for USB Boot Mode */
|
||||
#define VID 0x601A
|
||||
#define PID 0x4740
|
||||
|
||||
#define EP_BULK_TO 0x01
|
||||
#define TOUT 5000
|
||||
|
||||
enum USB_JZ4740_REQUEST
|
||||
{
|
||||
VR_GET_CPU_INFO = 0,
|
||||
VR_SET_DATA_ADDRESS,
|
||||
VR_SET_DATA_LENGTH,
|
||||
VR_FLUSH_CACHES,
|
||||
VR_PROGRAM_START1,
|
||||
VR_PROGRAM_START2,
|
||||
VR_NOR_OPS,
|
||||
VR_NAND_OPS,
|
||||
VR_SDRAM_OPS,
|
||||
VR_CONFIGURATION
|
||||
};
|
||||
|
||||
enum NOR_OPS_TYPE
|
||||
{
|
||||
NOR_INIT = 0,
|
||||
NOR_QUERY,
|
||||
NOR_WRITE,
|
||||
NOR_ERASE_CHIP,
|
||||
NOR_ERASE_SECTOR
|
||||
};
|
||||
|
||||
enum NOR_FLASH_TYPE
|
||||
{
|
||||
NOR_AM29 = 0,
|
||||
NOR_SST28,
|
||||
NOR_SST39x16,
|
||||
NOR_SST39x8
|
||||
};
|
||||
|
||||
enum NAND_OPS_TYPE
|
||||
{
|
||||
NAND_QUERY = 0,
|
||||
NAND_INIT,
|
||||
NAND_MARK_BAD,
|
||||
NAND_READ_OOB,
|
||||
NAND_READ_RAW,
|
||||
NAND_ERASE,
|
||||
NAND_READ,
|
||||
NAND_PROGRAM,
|
||||
NAND_READ_TO_RAM
|
||||
};
|
||||
|
||||
enum SDRAM_OPS_TYPE
|
||||
{
|
||||
SDRAM_LOAD,
|
||||
|
||||
};
|
||||
|
||||
enum DATA_STRUCTURE_OB
|
||||
{
|
||||
DS_flash_info ,
|
||||
DS_hand
|
||||
};
|
||||
|
||||
enum OPTION
|
||||
{
|
||||
OOB_ECC,
|
||||
OOB_NO_ECC,
|
||||
NO_OOB,
|
||||
};
|
||||
|
||||
int filesize(FILE* fd)
|
||||
{
|
||||
int tmp;
|
||||
fseek(fd, 0, SEEK_END);
|
||||
tmp = ftell(fd);
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
#define SEND_COMMAND(cmd, arg) err = usb_control_msg(dh, USB_ENDPOINT_OUT | USB_TYPE_VENDOR, cmd, arg>>16, arg&0xFFFF, NULL, 0, TOUT);\
|
||||
if (err < 0) \
|
||||
{ \
|
||||
fprintf(stderr,"\n[ERR] Error sending control message (%d, %s)\n", err, usb_strerror()); \
|
||||
return -1; \
|
||||
}
|
||||
|
||||
#define GET_CPU_INFO(s) err = usb_control_msg(dh, USB_ENDPOINT_IN | USB_TYPE_VENDOR, VR_GET_CPU_INFO, 0, 0, s, 8, TOUT); \
|
||||
if (err < 0) \
|
||||
{ \
|
||||
fprintf(stderr,"\n[ERR] Error sending control message (%d, %s)\n", err, usb_strerror()); \
|
||||
return -1; \
|
||||
}
|
||||
|
||||
#define SEND_DATA(ptr, size) err = usb_bulk_write(dh, USB_ENDPOINT_OUT | EP_BULK_TO, ptr, size, TOUT); \
|
||||
if (err != size) \
|
||||
{ \
|
||||
fprintf(stderr,"\n[ERR] Error writing data\n"); \
|
||||
fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err)); \
|
||||
return -1; \
|
||||
}
|
||||
|
||||
#define GET_DATA(ptr, size) err = usb_bulk_read(dh, USB_ENDPOINT_IN | EP_BULK_TO, ptr, size, TOUT); \
|
||||
if (err != size) \
|
||||
{ \
|
||||
fprintf(stderr,"\n[ERR] Error writing data\n"); \
|
||||
fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err)); \
|
||||
return -1; \
|
||||
}
|
||||
|
||||
int upload_app(usb_dev_handle* dh, int address, unsigned char* p, int len, bool stage2)
|
||||
{
|
||||
int err;
|
||||
char buf[8];
|
||||
unsigned char* tmp_buf;
|
||||
|
||||
fprintf(stderr, "[INFO] GET_CPU_INFO: ");
|
||||
GET_CPU_INFO(buf);
|
||||
buf[8] = 0;
|
||||
fprintf(stderr, "%s\n", buf);
|
||||
#if 0
|
||||
fprintf(stderr, "[INFO] Flushing cache...");
|
||||
SEND_COMMAND(VR_FLUSH_CACHES, 0);
|
||||
fprintf(stderr, " Done!\n");
|
||||
#endif
|
||||
|
||||
fprintf(stderr, "[INFO] SET_DATA_ADDRESS to 0x%x...", address);
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, address);
|
||||
fprintf(stderr, " Done!\n");
|
||||
|
||||
fprintf(stderr, "[INFO] Sending data...");
|
||||
/* Must not split the file in several packages! */
|
||||
SEND_DATA(p, len);
|
||||
fprintf(stderr, " Done!\n");
|
||||
|
||||
fprintf(stderr, "[INFO] Verifying data...");
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, address);
|
||||
SEND_COMMAND(VR_SET_DATA_LENGTH, len);
|
||||
tmp_buf = malloc(len);
|
||||
if (tmp_buf == NULL)
|
||||
{
|
||||
fprintf(stderr, "\n[ERR] Could not allocate memory.\n");
|
||||
return -1;
|
||||
}
|
||||
GET_DATA(tmp_buf, len);
|
||||
if (memcmp(tmp_buf, p, len) != 0)
|
||||
fprintf(stderr, "\n[WARN] Sent data isn't the same as received data...\n");
|
||||
else
|
||||
fprintf(stderr, " Done!\n");
|
||||
free(tmp_buf);
|
||||
|
||||
fprintf(stderr, "[INFO] Booting device [STAGE%d]...", (stage2 ? 2 : 1));
|
||||
SEND_COMMAND((stage2 ? VR_PROGRAM_START2 : VR_PROGRAM_START1), (address+(stage2 ? 8 : 0)) );
|
||||
fprintf(stderr, " Done!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int read_data(usb_dev_handle* dh, int address, unsigned char *p, int len)
|
||||
{
|
||||
int err;
|
||||
char buf[8];
|
||||
|
||||
fprintf(stderr, "[INFO] GET_CPU_INFO: ");
|
||||
GET_CPU_INFO(buf);
|
||||
buf[8] = 0;
|
||||
fprintf(stderr, "%s\n", buf);
|
||||
|
||||
fprintf(stderr, "[INFO] Reading data...");
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, address);
|
||||
SEND_COMMAND(VR_SET_DATA_LENGTH, len);
|
||||
GET_DATA(p, len);
|
||||
fprintf(stderr, " Done!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int read_reg(usb_dev_handle* dh, int address, int size)
|
||||
{
|
||||
int err;
|
||||
unsigned char buf[4];
|
||||
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, address);
|
||||
SEND_COMMAND(VR_SET_DATA_LENGTH, size);
|
||||
GET_DATA(buf, size);
|
||||
|
||||
if(size == 1)
|
||||
return buf[0];
|
||||
else if(size == 2)
|
||||
return (buf[1] << 8) | buf[0];
|
||||
else if(size == 4)
|
||||
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_reg(usb_dev_handle* dh, int address, unsigned int val, int size)
|
||||
{
|
||||
int err, i;
|
||||
unsigned char buf[4];
|
||||
|
||||
buf[0] = val & 0xff;
|
||||
if(i > 1)
|
||||
{
|
||||
buf[1] = (val >> 8) & 0xff;
|
||||
if(i > 2)
|
||||
{
|
||||
buf[2] = (val >> 16) & 0xff;
|
||||
buf[3] = (val >> 24) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, address);
|
||||
SEND_DATA(buf, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#define or_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) | (val)), size);
|
||||
#define and_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) & (val)), size);
|
||||
#define bc_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) & ~(val)), size);
|
||||
#define xor_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) ^ (val)), size);
|
||||
|
||||
#define TEST(m, size) fprintf(stderr, "%s -> %x\n", #m, read_reg(dh, m, size));
|
||||
int test_device(usb_dev_handle* dh)
|
||||
{
|
||||
TEST(INTC_ISR, 4);
|
||||
TEST(INTC_IMR, 4);
|
||||
TEST(INTC_IMSR, 4);
|
||||
TEST(INTC_IMCR, 4);
|
||||
TEST(INTC_IPR, 4);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
TEST(RTC_RCR, 4);
|
||||
TEST(RTC_RSR, 4);
|
||||
TEST(RTC_RSAR, 4);
|
||||
TEST(RTC_RGR, 4);
|
||||
TEST(RTC_HCR, 4);
|
||||
TEST(RTC_RCR, 4);
|
||||
TEST(RTC_HWFCR, 4);
|
||||
TEST(RTC_HRCR, 4);
|
||||
TEST(RTC_HWCR, 4);
|
||||
TEST(RTC_HWSR, 4);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
TEST(GPIO_PXPIN(0), 4);
|
||||
TEST(GPIO_PXPIN(1), 4);
|
||||
TEST(GPIO_PXPIN(2), 4);
|
||||
TEST(GPIO_PXPIN(3), 4);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
TEST(CPM_CLKGR, 4);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
//or_reg(dh, SADC_ENA, SADC_ENA_TSEN, 1);
|
||||
TEST(SADC_ENA, 1);
|
||||
TEST(SADC_CTRL, 1);
|
||||
TEST(SADC_TSDAT, 4);
|
||||
TEST(SADC_BATDAT, 2);
|
||||
TEST(SADC_STATE, 1);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
TEST(SLCD_CFG, 4);
|
||||
TEST(SLCD_CTRL, 1);
|
||||
TEST(SLCD_STATE, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define VOL_DOWN (1 << 27)
|
||||
#define VOL_UP (1 << 0)
|
||||
#define MENU (1 << 1)
|
||||
#define HOLD (1 << 16)
|
||||
#define OFF (1 << 29)
|
||||
#define MASK (VOL_DOWN|VOL_UP|MENU|HOLD|OFF)
|
||||
#define TS_MASK (SADC_STATE_PEND|SADC_STATE_PENU|SADC_STATE_TSRDY)
|
||||
int probe_device(usb_dev_handle* dh)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
//or_reg(dh, SADC_ENA, SADC_ENA_TSEN, 1);
|
||||
while(1)
|
||||
{
|
||||
if(read_reg(dh, SADC_STATE, 1) & SADC_STATE_TSRDY)
|
||||
{
|
||||
printf("%x\n", read_reg(dh, SADC_TSDAT, 4));
|
||||
or_reg(dh, SADC_CTRL, read_reg(dh, SADC_STATE, 1) & TS_MASK, 1);
|
||||
}
|
||||
|
||||
tmp = read_reg(dh, GPIO_PXPIN(3), 4);
|
||||
if(tmp < 0)
|
||||
return tmp;
|
||||
if(tmp ^ MASK)
|
||||
{
|
||||
if(!(tmp & VOL_DOWN))
|
||||
printf("VOL_DOWN\t");
|
||||
if(!(tmp & VOL_UP))
|
||||
printf("VOL_UP\t");
|
||||
if(!(tmp & MENU))
|
||||
printf("MENU\t");
|
||||
if(!(tmp & OFF))
|
||||
printf("OFF\t");
|
||||
if(!(tmp & HOLD))
|
||||
printf("HOLD\t");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int read_file(const char *name, unsigned char **buffer)
|
||||
{
|
||||
FILE *fd;
|
||||
int len, n;
|
||||
|
||||
fd = fopen(name, "rb");
|
||||
if (fd < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not open %s\n", name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
len = filesize(fd);
|
||||
|
||||
*buffer = (unsigned char*)malloc(len);
|
||||
if (*buffer == NULL)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not allocate memory.\n");
|
||||
fclose(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
n = fread(*buffer, 1, len, fd);
|
||||
if (n != len)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Short read.\n");
|
||||
fclose(fd);
|
||||
return 0;
|
||||
}
|
||||
fclose(fd);
|
||||
|
||||
return len;
|
||||
}
|
||||
#define _GET_CPU fprintf(stderr, "[INFO] GET_CPU_INFO:"); \
|
||||
GET_CPU_INFO(cpu); \
|
||||
cpu[8] = 0; \
|
||||
fprintf(stderr, " %s\n", cpu);
|
||||
#define _SET_ADDR(a) fprintf(stderr, "[INFO] Set address to 0x%x...", a); \
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, a); \
|
||||
fprintf(stderr, " Done!\n");
|
||||
#define _SEND_FILE(a) fsize = read_file(a, &buffer); \
|
||||
fprintf(stderr, "[INFO] Sending file %s: %d bytes...", a, fsize); \
|
||||
SEND_DATA(buffer, fsize); \
|
||||
free(buffer); \
|
||||
fprintf(stderr, " Done!\n");
|
||||
#define _VERIFY_DATA(a,c) fprintf(stderr, "[INFO] Verifying data (%s)...", a); \
|
||||
fsize = read_file(a, &buffer); \
|
||||
buffer2 = (unsigned char*)malloc(fsize); \
|
||||
SEND_COMMAND(VR_SET_DATA_ADDRESS, c); \
|
||||
SEND_COMMAND(VR_SET_DATA_LENGTH, fsize); \
|
||||
GET_DATA(buffer2, fsize); \
|
||||
if(memcmp(buffer, buffer2, fsize) != 0) \
|
||||
fprintf(stderr, "\n[WARN] Sent data isn't the same as received data...\n"); \
|
||||
else \
|
||||
fprintf(stderr, " Done!\n"); \
|
||||
free(buffer); \
|
||||
free(buffer2);
|
||||
#define _STAGE1(a) fprintf(stderr, "[INFO] Stage 1 at 0x%x\n", a); \
|
||||
SEND_COMMAND(VR_PROGRAM_START1, a);
|
||||
#define _STAGE2(a) fprintf(stderr, "[INFO] Stage 2 at 0x%x\n", a); \
|
||||
SEND_COMMAND(VR_PROGRAM_START2, a);
|
||||
#define _FLUSH fprintf(stderr, "[INFO] Flushing caches...\n"); \
|
||||
SEND_COMMAND(VR_FLUSH_CACHES, 0);
|
||||
#ifdef _WIN32
|
||||
#define _SLEEP(x) Sleep(x*1000);
|
||||
#else
|
||||
#define _SLEEP(x) sleep(x);
|
||||
#endif
|
||||
int mimic_of(usb_dev_handle *dh)
|
||||
{
|
||||
int err, fsize;
|
||||
unsigned char *buffer, *buffer2;
|
||||
char cpu[8];
|
||||
|
||||
fprintf(stderr, "[INFO] Start!\n");
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x8000 << 16);
|
||||
_SEND_FILE("1.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("1.bin", 0x8000 << 16);
|
||||
_STAGE1(0x8000 << 16);
|
||||
_SLEEP(3);
|
||||
_VERIFY_DATA("2.bin", 0xB3020060);
|
||||
_GET_CPU;
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x8000 << 16);
|
||||
_SEND_FILE("3.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("3.bin", 0x8000 << 16);
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x80D0 << 16);
|
||||
_SEND_FILE("4.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("4.bin", 0x80D0 << 16);
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x80E0 << 16);
|
||||
_SEND_FILE("5.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("5.bin", 0x80E0 << 16);
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x80004000);
|
||||
_SEND_FILE("6.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("6.bin", 0x80004000);
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x80FD << 16);
|
||||
_SEND_FILE("7.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("7.bin", 0x80FD << 16);
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_STAGE2(0x80FD0004);
|
||||
_VERIFY_DATA("8.bin", 0x80004004);
|
||||
_VERIFY_DATA("9.bin", 0x80004008);
|
||||
_SLEEP(2);
|
||||
_GET_CPU;
|
||||
_SET_ADDR(0x80E0 << 16);
|
||||
_SEND_FILE("10.bin");
|
||||
_GET_CPU;
|
||||
_VERIFY_DATA("10.bin", 0x80E0 << 16);
|
||||
_GET_CPU;
|
||||
_FLUSH;
|
||||
_GET_CPU;
|
||||
_STAGE2(0x80e00008);
|
||||
fprintf(stderr, "[INFO] Done!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void jzconnect(int address, unsigned char* buf, int len, int func)
|
||||
{
|
||||
struct usb_bus *bus;
|
||||
struct usb_device *tmp_dev;
|
||||
struct usb_device *dev = NULL;
|
||||
usb_dev_handle *dh;
|
||||
int err;
|
||||
|
||||
fprintf(stderr,"[INFO] Searching for device...\n");
|
||||
|
||||
usb_init();
|
||||
if(usb_find_busses() < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not find any USB busses.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (usb_find_devices() < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (bus = usb_get_busses(); bus; bus = bus->next)
|
||||
{
|
||||
for (tmp_dev = bus->devices; tmp_dev; tmp_dev = tmp_dev->next)
|
||||
{
|
||||
//printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
|
||||
if (tmp_dev->descriptor.idVendor == VID &&
|
||||
tmp_dev->descriptor.idProduct == PID)
|
||||
{
|
||||
dev = tmp_dev;
|
||||
goto found;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dev == NULL)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Device not found.\n");
|
||||
fprintf(stderr, "[ERR] Ensure your device is in USB boot mode and run usbtool again.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
found:
|
||||
if ( (dh = usb_open(dev)) == NULL)
|
||||
{
|
||||
fprintf(stderr,"[ERR] Unable to open device.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
err = usb_set_configuration(dh, 1);
|
||||
|
||||
if (err < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] usb_set_configuration failed (%d, %s)\n", err, usb_strerror());
|
||||
usb_close(dh);
|
||||
return;
|
||||
}
|
||||
|
||||
/* "must be called" written in the libusb documentation */
|
||||
err = usb_claim_interface(dh, 0);
|
||||
if (err < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Unable to claim interface (%d, %s)\n", err, usb_strerror());
|
||||
usb_close(dh);
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stderr,"[INFO] Found device, uploading application.\n");
|
||||
|
||||
/* Now we can transfer the application to the device. */
|
||||
|
||||
switch(func)
|
||||
{
|
||||
case 1:
|
||||
case 5:
|
||||
err = upload_app(dh, address, buf, len, (func == 5));
|
||||
break;
|
||||
case 2:
|
||||
err = read_data(dh, address, buf, len);
|
||||
break;
|
||||
case 3:
|
||||
err = test_device(dh);
|
||||
break;
|
||||
case 4:
|
||||
err = probe_device(dh);
|
||||
break;
|
||||
case 6:
|
||||
err = mimic_of(dh);
|
||||
break;
|
||||
}
|
||||
|
||||
/* release claimed interface */
|
||||
usb_release_interface(dh, 0);
|
||||
|
||||
usb_close(dh);
|
||||
}
|
||||
|
||||
void print_usage(void)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
fprintf(stderr, "Usage: usbtool.exe [CMD] [FILE] [ADDRESS] [LEN]\n");
|
||||
#else
|
||||
fprintf(stderr, "Usage: usbtool [CMD] [FILE] [ADDRESS] [LEN]\n");
|
||||
#endif
|
||||
fprintf(stderr, "\t[ADDRESS] has to be in 0xHEXADECIMAL format\n");
|
||||
fprintf(stderr, "\t[CMD]:\n\t\t1 -> upload file to specified address and boot from it\n\t\t2 -> read data from [ADDRESS] with length [LEN] to [FILE]\n");
|
||||
fprintf(stderr, "\t\t3 -> read device status\n\t\t4 -> probe keys (only Onda VX747)\n");
|
||||
fprintf(stderr, "\t\t5 -> same as 1 but do a stage 2 boot\n\t\t6 -> mimic OF fw recovery\n");
|
||||
#ifdef _WIN32
|
||||
fprintf(stderr, "\nExample:\n\t usbtool.exe 1 fw.bin 0x80000000");
|
||||
fprintf(stderr, "\n\t usbtool.exe 2 save.bin 0x81000000 1024");
|
||||
#else
|
||||
fprintf(stderr, "\nExample:\n\t usbtool 1 fw.bin 0x80000000");
|
||||
fprintf(stderr, "\n\t usbtool 2 save.bin 0x81000000 1024");
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
unsigned char* buf;
|
||||
int n, len, address, cmd=0;
|
||||
FILE* fd;
|
||||
|
||||
fprintf(stderr, "USBtool 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 > 1)
|
||||
sscanf(argv[1], "%d", &cmd);
|
||||
switch(cmd)
|
||||
{
|
||||
case 5:
|
||||
case 1:
|
||||
if (strcmp(argv[3], "-1") == 0)
|
||||
address = 0x80000000;
|
||||
else
|
||||
{
|
||||
if (sscanf(argv[3], "0x%x", &address) <= 0)
|
||||
{
|
||||
print_usage();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
fd = fopen(argv[2], "rb");
|
||||
if (fd < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not open %s\n", argv[2]);
|
||||
return 4;
|
||||
}
|
||||
|
||||
len = filesize(fd);
|
||||
|
||||
if (len > MAX_FIRMWARESIZE)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Firmware file too big\n");
|
||||
fclose(fd);
|
||||
return 5;
|
||||
}
|
||||
|
||||
buf = malloc(len);
|
||||
if (buf == NULL)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not allocate memory.\n");
|
||||
fclose(fd);
|
||||
return 6;
|
||||
}
|
||||
|
||||
n = fread(buf, 1, len, fd);
|
||||
if (n != len)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Short read.\n");
|
||||
fclose(fd);
|
||||
return 7;
|
||||
}
|
||||
fclose(fd);
|
||||
|
||||
fprintf(stderr, "[INFO] File size: %d bytes\n", n);
|
||||
|
||||
jzconnect(address, buf, len, cmd);
|
||||
break;
|
||||
case 2:
|
||||
if (sscanf(argv[3], "0x%x", &address) <= 0)
|
||||
{
|
||||
print_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd = fopen(argv[2], "wb");
|
||||
if (fd < 0)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not open %s\n", argv[2]);
|
||||
return 4;
|
||||
}
|
||||
|
||||
sscanf(argv[4], "%d", &len);
|
||||
|
||||
buf = malloc(len);
|
||||
if (buf == NULL)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Could not allocate memory.\n");
|
||||
fclose(fd);
|
||||
return 6;
|
||||
}
|
||||
|
||||
jzconnect(address, buf, len, 2);
|
||||
|
||||
n = fwrite(buf, 1, len, fd);
|
||||
if (n != len)
|
||||
{
|
||||
fprintf(stderr, "[ERR] Short write.\n");
|
||||
fclose(fd);
|
||||
return 7;
|
||||
}
|
||||
fclose(fd);
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
case 6:
|
||||
jzconnect(address, NULL, 0, cmd);
|
||||
break;
|
||||
default:
|
||||
print_usage();
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
3
utils/jz4740_tools/windows_driver/jz4740_usbtool.cat
Executable file
3
utils/jz4740_tools/windows_driver/jz4740_usbtool.cat
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
This file will contain the digital signature of the files to be installed
|
||||
on the system.
|
||||
This file will be provided by Microsoft upon certification of your drivers.
|
||||
136
utils/jz4740_tools/windows_driver/jz4740_usbtool.inf
Executable file
136
utils/jz4740_tools/windows_driver/jz4740_usbtool.inf
Executable file
|
|
@ -0,0 +1,136 @@
|
|||
[Version]
|
||||
Signature = "$Chicago$"
|
||||
provider = %manufacturer%
|
||||
DriverVer = 03/20/2007,0.1.12.1
|
||||
CatalogFile = jz4740_usbtool.cat
|
||||
CatalogFile.NT = jz4740_usbtool.cat
|
||||
CatalogFile.NTAMD64 = jz4740_usbtool_x64.cat
|
||||
|
||||
Class = LibUsbDevices
|
||||
ClassGUID = {EB781AAF-9C70-4523-A5DF-642A87ECA567}
|
||||
|
||||
[ClassInstall]
|
||||
AddReg=libusb_class_install_add_reg
|
||||
|
||||
[ClassInstall32]
|
||||
AddReg=libusb_class_install_add_reg
|
||||
|
||||
[libusb_class_install_add_reg]
|
||||
HKR,,,,"LibUSB-Win32 Devices"
|
||||
HKR,,Icon,,"-20"
|
||||
|
||||
[Manufacturer]
|
||||
%manufacturer%=Devices,NT,NTAMD64
|
||||
|
||||
;--------------------------------------------------------------------------
|
||||
; Files
|
||||
;--------------------------------------------------------------------------
|
||||
|
||||
[SourceDisksNames]
|
||||
1 = "Libusb-Win32 Driver Installation Disk",,
|
||||
|
||||
[SourceDisksFiles]
|
||||
libusb0.sys = 1,,
|
||||
libusb0.dll = 1,,
|
||||
libusb0_x64.sys = 1,,
|
||||
libusb0_x64.dll = 1,,
|
||||
|
||||
[DestinationDirs]
|
||||
libusb_files_sys = 10,system32\drivers
|
||||
libusb_files_sys_x64 = 10,system32\drivers
|
||||
libusb_files_dll = 10,system32
|
||||
libusb_files_dll_wow64 = 10,syswow64
|
||||
libusb_files_dll_x64 = 10,system32
|
||||
|
||||
[libusb_files_sys]
|
||||
libusb0.sys
|
||||
|
||||
[libusb_files_sys_x64]
|
||||
libusb0.sys,libusb0_x64.sys
|
||||
|
||||
[libusb_files_dll]
|
||||
libusb0.dll
|
||||
|
||||
[libusb_files_dll_wow64]
|
||||
libusb0.dll
|
||||
|
||||
[libusb_files_dll_x64]
|
||||
libusb0.dll,libusb0_x64.dll
|
||||
|
||||
;--------------------------------------------------------------------------
|
||||
; Device driver
|
||||
;--------------------------------------------------------------------------
|
||||
|
||||
[LIBUSB_DEV]
|
||||
CopyFiles = libusb_files_sys, libusb_files_dll
|
||||
AddReg = libusb_add_reg
|
||||
|
||||
[LIBUSB_DEV.NT]
|
||||
CopyFiles = libusb_files_sys, libusb_files_dll
|
||||
|
||||
[LIBUSB_DEV.NTAMD64]
|
||||
CopyFiles = libusb_files_sys_x64, libusb_files_dll_wow64, libusb_files_dll_x64
|
||||
|
||||
[LIBUSB_DEV.HW]
|
||||
DelReg = libusb_del_reg_hw
|
||||
AddReg = libusb_add_reg_hw
|
||||
|
||||
[LIBUSB_DEV.NT.HW]
|
||||
DelReg = libusb_del_reg_hw
|
||||
AddReg = libusb_add_reg_hw
|
||||
|
||||
[LIBUSB_DEV.NTAMD64.HW]
|
||||
DelReg = libusb_del_reg_hw
|
||||
AddReg = libusb_add_reg_hw
|
||||
|
||||
[LIBUSB_DEV.NT.Services]
|
||||
AddService = libusb0, 0x00000002, libusb_add_service
|
||||
|
||||
[LIBUSB_DEV.NTAMD64.Services]
|
||||
AddService = libusb0, 0x00000002, libusb_add_service
|
||||
|
||||
[libusb_add_reg]
|
||||
HKR,,DevLoader,,*ntkern
|
||||
HKR,,NTMPDriver,,libusb0.sys
|
||||
|
||||
; Older versions of this .inf file installed filter drivers. They are not
|
||||
; needed any more and must be removed
|
||||
[libusb_del_reg_hw]
|
||||
HKR,,LowerFilters
|
||||
HKR,,UpperFilters
|
||||
|
||||
; Device properties
|
||||
[libusb_add_reg_hw]
|
||||
HKR,,SurpriseRemovalOK, 0x00010001, 1
|
||||
|
||||
;--------------------------------------------------------------------------
|
||||
; Services
|
||||
;--------------------------------------------------------------------------
|
||||
|
||||
[libusb_add_service]
|
||||
DisplayName = "LibUsb-Win32 - Kernel Driver 03/20/2007, 0.1.12.1"
|
||||
ServiceType = 1
|
||||
StartType = 3
|
||||
ErrorControl = 0
|
||||
ServiceBinary = %12%\libusb0.sys
|
||||
|
||||
;--------------------------------------------------------------------------
|
||||
; Devices
|
||||
;--------------------------------------------------------------------------
|
||||
|
||||
[Devices]
|
||||
"Jz4740 USB Boot Device (Rockbox USBtool)"=LIBUSB_DEV, USB\VID_601a&PID_4740
|
||||
|
||||
[Devices.NT]
|
||||
"Jz4740 USB Boot Device (Rockbox USBtool)"=LIBUSB_DEV, USB\VID_601a&PID_4740
|
||||
|
||||
[Devices.NTAMD64]
|
||||
"Jz4740 USB Boot Device (Rockbox USBtool)"=LIBUSB_DEV, USB\VID_601a&PID_4740
|
||||
|
||||
|
||||
;--------------------------------------------------------------------------
|
||||
; Strings
|
||||
;--------------------------------------------------------------------------
|
||||
|
||||
[Strings]
|
||||
manufacturer = "Ingenic"
|
||||
3
utils/jz4740_tools/windows_driver/jz4740_usbtool_x64.cat
Executable file
3
utils/jz4740_tools/windows_driver/jz4740_usbtool_x64.cat
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
This file will contain the digital signature of the files to be installed
|
||||
on the system.
|
||||
This file will be provided by Microsoft upon certification of your drivers.
|
||||
BIN
utils/jz4740_tools/windows_driver/libusb0.dll
Executable file
BIN
utils/jz4740_tools/windows_driver/libusb0.dll
Executable file
Binary file not shown.
BIN
utils/jz4740_tools/windows_driver/libusb0.sys
Executable file
BIN
utils/jz4740_tools/windows_driver/libusb0.sys
Executable file
Binary file not shown.
BIN
utils/jz4740_tools/windows_driver/libusb0_x64.dll
Executable file
BIN
utils/jz4740_tools/windows_driver/libusb0_x64.dll
Executable file
Binary file not shown.
BIN
utils/jz4740_tools/windows_driver/libusb0_x64.sys
Executable file
BIN
utils/jz4740_tools/windows_driver/libusb0_x64.sys
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue