Fix checksum issues
[Rockbox.git] / utils / jz4740_tools / HXFreplace.c
blobfc97a3c1e882caab79f602f4a8922ce92450954f
1 /*
2 Made by Maurus Cuelenaere
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <stdbool.h>
13 #include <dirent.h>
15 #define VERSION "0.1"
17 static unsigned char* int2le(unsigned int val)
19 static unsigned char addr[4];
20 addr[0] = val & 0xff;
21 addr[1] = (val >> 8) & 0xff;
22 addr[2] = (val >> 16) & 0xff;
23 addr[3] = (val >> 24) & 0xff;
24 return addr;
27 static unsigned int le2int(unsigned char* buf)
29 unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
31 return res;
34 unsigned int _filesize(FILE* fd)
36 unsigned int tmp, oldpos;
37 oldpos = ftell(fd);
38 fseek(fd, 0, SEEK_END);
39 tmp = ftell(fd);
40 fseek(fd, oldpos, SEEK_SET);
41 return tmp;
44 static void print_usage(void)
46 #ifdef _WIN32
47 fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
48 fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n");
49 #else
50 fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n");
51 fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n");
52 #endif
55 static int checksum(FILE *file)
57 int oldpos = ftell(file);
58 int ret=0, i, filesize = _filesize(file)-0x40;
59 unsigned char *buf;
61 buf = (unsigned char*)malloc(filesize);
63 if(buf == NULL)
65 fseek(file, oldpos, SEEK_SET);
66 fprintf(stderr, "[ERR] Error while allocating memory\n");
67 return 0;
70 fseek(file, 0x40, SEEK_SET);
71 if(fread(buf, filesize, 1, file) != 1)
73 free(buf);
74 fseek(file, oldpos, SEEK_SET);
75 fprintf(stderr, "[ERR] Error while reading from file\n");
76 return 0;
79 fprintf(stderr, "[INFO] Computing checksum...");
81 for(i = 0; i < filesize; i+=4)
82 ret += le2int(&buf[i]);
84 free(buf);
85 fseek(file, oldpos, SEEK_SET);
87 fprintf(stderr, " Done!\n");
88 return ret;
91 int main(int argc, char *argv[])
93 FILE *infile, *outfile, *fw;
95 fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n");
96 fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n");
97 fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
99 if(argc != 4)
101 print_usage();
102 return 1;
105 if((infile = fopen(argv[1], "rb")) == NULL)
107 fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]);
108 return 2;
111 if(fseek(infile, 0x40, SEEK_SET) != 0)
113 fprintf(stderr, "[ERR] Cannot seek to 0x40\n");
114 fclose(infile);
115 return 3;
118 fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n");
120 int found = -1;
121 int filenamesize;
122 char *filename;
123 unsigned char tmp[4];
125 #define READ(x, len) if(fread(x, len, 1, infile) != 1) \
127 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \
128 fclose(infile); \
129 return 4; \
131 while(found < 0)
133 READ(&tmp[0], 4);
134 filenamesize = le2int(tmp);
135 filename = (char*)malloc(filenamesize);
136 READ(filename, filenamesize);
137 if(strcmp(filename, "ccpmp.bin") == 0)
138 found = ftell(infile);
139 else
141 READ(&tmp[0], 4);
142 fseek(infile, le2int(tmp), SEEK_CUR);
144 free(filename);
147 fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found);
149 if((outfile = fopen(argv[2], "wb+")) == NULL)
151 fclose(infile);
152 fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]);
153 return 5;
156 #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \
158 fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \
159 fclose(outfile); \
160 if(fw != NULL) \
161 fclose(fw); \
162 return 5; \
165 unsigned char* buffer;
167 buffer = (unsigned char*)malloc(found);
168 fseek(infile, 0, SEEK_SET);
169 READ(buffer, found);
170 WRITE(buffer, found);
171 free(buffer);
173 if((fw = fopen(argv[3], "rb")) == NULL)
175 fclose(infile);
176 fclose(outfile);
177 fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]);
180 int fw_filesize = _filesize(fw);
182 #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \
184 fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \
185 fclose(infile); \
186 fclose(outfile); \
187 return 6; \
189 buffer = (unsigned char*)malloc(fw_filesize);
190 READ2(buffer, fw_filesize);
191 fputc(0x20, outfile); /* Padding */
192 WRITE(int2le(fw_filesize), 4);
193 WRITE(buffer, fw_filesize);
194 free(buffer);
195 fclose(fw);
196 fw = NULL;
198 fseek(infile, found+1, SEEK_SET);
199 READ(&tmp, 4);
200 if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0)
202 fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]);
203 fclose(infile);
204 fclose(outfile);
205 return 7;
207 found = ftell(infile);
209 int other_size = _filesize(infile) - found;
210 buffer = (unsigned char*)malloc(other_size);
211 READ(buffer, other_size);
212 WRITE(buffer, other_size);
213 free(buffer);
214 fclose(infile);
216 fflush(outfile);
217 fseek(outfile, 0x14, SEEK_SET);
218 WRITE(int2le(_filesize(outfile)), 4);
219 WRITE(int2le(checksum(outfile)), 4);
220 fclose(outfile);
222 fprintf(stderr, "[INFO] Done!\n");
224 return 0;