MPEGPlayer: Try out a different frame drop scheme meant to skip in a more uniform...
[maemo-rb.git] / utils / jz4740_tools / HXFreplace.c
blobbcfff825409f9046fa58e660b3eef79b4d52a356
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <stdbool.h>
30 #include <dirent.h>
32 #define VERSION "0.1"
34 static unsigned char* int2le(unsigned int val)
36 static unsigned char addr[4];
37 addr[0] = val & 0xff;
38 addr[1] = (val >> 8) & 0xff;
39 addr[2] = (val >> 16) & 0xff;
40 addr[3] = (val >> 24) & 0xff;
41 return addr;
44 static unsigned int le2int(unsigned char* buf)
46 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
48 return res;
51 unsigned int _filesize(FILE* fd)
53 unsigned int tmp, oldpos;
54 oldpos = ftell(fd);
55 fseek(fd, 0, SEEK_END);
56 tmp = ftell(fd);
57 fseek(fd, oldpos, SEEK_SET);
58 return tmp;
61 static void print_usage(void)
63 #ifdef _WIN32
64 fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
65 fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
66 #else
67 fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
68 fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n");
69 #endif
72 static int checksum(FILE *file)
74 int oldpos = ftell(file);
75 int ret=0, i, filesize = _filesize(file)-0x40;
76 unsigned char *buf;
78 buf = (unsigned char*)malloc(filesize);
80 if(buf == NULL)
82 fseek(file, oldpos, SEEK_SET);
83 fprintf(stderr, "[ERR] Error while allocating memory\n");
84 return 0;
87 fseek(file, 0x40, SEEK_SET);
88 if(fread(buf, filesize, 1, file) != 1)
90 free(buf);
91 fseek(file, oldpos, SEEK_SET);
92 fprintf(stderr, "[ERR] Error while reading from file\n");
93 return 0;
96 fprintf(stderr, "[INFO] Computing checksum...");
98 for(i = 0; i < filesize; i+=4)
99 ret += le2int(&buf[i]);
101 free(buf);
102 fseek(file, oldpos, SEEK_SET);
104 fprintf(stderr, " Done!\n");
105 return ret;
108 int main(int argc, char *argv[])
110 FILE *infile, *outfile, *fw;
112 fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
113 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
114 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
116 if(argc != 4)
118 print_usage();
119 return 1;
122 if((infile = fopen(argv[1], "rb")) == NULL)
124 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
125 return 2;
128 if(fseek(infile, 0x40, SEEK_SET) != 0)
130 fprintf(stderr, "[ERR] Cannot seek to 0x40\n");
131 fclose(infile);
132 return 3;
135 fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n");
137 int found = -1;
138 int filenamesize;
139 char *filename;
140 unsigned char tmp[4];
142 #define READ(x, len) if(fread(x, len, 1, infile) != 1) \
144 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
145 fclose(infile); \
146 return 4; \
148 while(found < 0)
150 READ(&tmp[0], 4);
151 filenamesize = le2int(tmp);
152 filename = (char*)malloc(filenamesize);
153 READ(filename, filenamesize);
154 if(strcmp(filename, "ccpmp.bin") == 0)
155 found = ftell(infile);
156 else
158 READ(&tmp[0], 4);
159 fseek(infile, le2int(tmp), SEEK_CUR);
161 free(filename);
164 fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found);
166 if((outfile = fopen(argv[2], "wb+")) == NULL)
168 fclose(infile);
169 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
170 return 5;
173 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
175 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
176 fclose(outfile); \
177 if(fw != NULL) \
178 fclose(fw); \
179 return 5; \
182 unsigned char* buffer;
184 buffer = (unsigned char*)malloc(found);
185 fseek(infile, 0, SEEK_SET);
186 READ(buffer, found);
187 WRITE(buffer, found);
188 free(buffer);
190 if((fw = fopen(argv[3], "rb")) == NULL)
192 fclose(infile);
193 fclose(outfile);
194 fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]);
197 int fw_filesize = _filesize(fw);
199 #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
201 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
202 fclose(infile); \
203 fclose(outfile); \
204 return 6; \
206 buffer = (unsigned char*)malloc(fw_filesize);
207 READ2(buffer, fw_filesize);
208 fputc(0x20, outfile); /* Padding */
209 WRITE(int2le(fw_filesize), 4);
210 WRITE(buffer, fw_filesize);
211 free(buffer);
212 fclose(fw);
213 fw = NULL;
215 fseek(infile, found+1, SEEK_SET);
216 READ(&tmp, 4);
217 if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0)
219 fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]);
220 fclose(infile);
221 fclose(outfile);
222 return 7;
224 found = ftell(infile);
226 int other_size = _filesize(infile) - found;
227 buffer = (unsigned char*)malloc(other_size);
228 READ(buffer, other_size);
229 WRITE(buffer, other_size);
230 free(buffer);
231 fclose(infile);
233 fflush(outfile);
234 fseek(outfile, 0x14, SEEK_SET);
235 WRITE(int2le(_filesize(outfile)), 4);
236 WRITE(int2le(checksum(outfile)), 4);
237 fclose(outfile);
239 fprintf(stderr, "[INFO] Done!\n");
241 return 0;