diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2008-06-28 16:49:46 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2008-06-28 16:49:46 +0000 |
commit | d951e169f5f4e82590b36b1e703a09e71da1fdd1 (patch) | |
tree | 45480d4388cc307c97cfba0edd31a2f07e4c34f6 /utils/jz4740_tools/HXFreplace.c | |
parent | 9c84070c8a56b89b32c92eeca06154ac6ad927a5 (diff) | |
download | rockbox-d951e169f5f4e82590b36b1e703a09e71da1fdd1.tar.gz rockbox-d951e169f5f4e82590b36b1e703a09e71da1fdd1.zip |
Rename jz4740 tools + add some new ones
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17840 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/jz4740_tools/HXFreplace.c')
-rwxr-xr-x | utils/jz4740_tools/HXFreplace.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/utils/jz4740_tools/HXFreplace.c b/utils/jz4740_tools/HXFreplace.c new file mode 100755 index 0000000000..a33a729a1c --- /dev/null +++ b/utils/jz4740_tools/HXFreplace.c | |||
@@ -0,0 +1,225 @@ | |||
1 | /* | ||
2 | Made by Maurus Cuelenaere | ||
3 | */ | ||
4 | |||
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> | ||
14 | |||
15 | #define VERSION "0.1" | ||
16 | |||
17 | static unsigned char* int2le(unsigned int val) | ||
18 | { | ||
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; | ||
25 | } | ||
26 | |||
27 | static unsigned int le2int(unsigned char* buf) | ||
28 | { | ||
29 | unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; | ||
30 | |||
31 | return res; | ||
32 | } | ||
33 | |||
34 | unsigned int _filesize(FILE* fd) | ||
35 | { | ||
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; | ||
42 | } | ||
43 | |||
44 | static void print_usage(void) | ||
45 | { | ||
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 | ||
53 | } | ||
54 | |||
55 | static unsigned int checksum(FILE *file) | ||
56 | { | ||
57 | int oldpos = ftell(file); | ||
58 | unsigned int ret, i, filesize = _filesize(file)-0x40; | ||
59 | unsigned char *buf; | ||
60 | |||
61 | buf = (unsigned char*)malloc(filesize); | ||
62 | |||
63 | if(buf == NULL) | ||
64 | { | ||
65 | fseek(file, oldpos, SEEK_SET); | ||
66 | fprintf(stderr, "[ERR] Error while allocating memory\n"); | ||
67 | return 0; | ||
68 | } | ||
69 | |||
70 | fseek(file, 0x40, SEEK_SET); | ||
71 | if(fread(buf, filesize, 1, file) != 1) | ||
72 | { | ||
73 | free(buf); | ||
74 | fseek(file, oldpos, SEEK_SET); | ||
75 | fprintf(stderr, "[ERR] Error while reading from file\n"); | ||
76 | return 0; | ||
77 | } | ||
78 | |||
79 | fprintf(stderr, "[INFO] Computing checksum..."); | ||
80 | |||
81 | for(i = 0; i < filesize; i+=4) | ||
82 | ret += le2int(&buf[i]); | ||
83 | |||
84 | free(buf); | ||
85 | fseek(file, oldpos, SEEK_SET); | ||
86 | |||
87 | fprintf(stderr, " Done!\n"); | ||
88 | return ret; | ||
89 | } | ||
90 | |||
91 | int main(int argc, char *argv[]) | ||
92 | { | ||
93 | FILE *infile, *outfile, *fw; | ||
94 | |||
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"); | ||
98 | |||
99 | if(argc != 4) | ||
100 | { | ||
101 | print_usage(); | ||
102 | return 1; | ||
103 | } | ||
104 | |||
105 | if((infile = fopen(argv[1], "rb")) == NULL) | ||
106 | { | ||
107 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); | ||
108 | return 2; | ||
109 | } | ||
110 | |||
111 | if(fseek(infile, 0x40, SEEK_SET) != 0) | ||
112 | { | ||
113 | fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); | ||
114 | fclose(infile); | ||
115 | return 3; | ||
116 | } | ||
117 | |||
118 | fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); | ||
119 | |||
120 | int found = -1; | ||
121 | int filenamesize; | ||
122 | char *filename; | ||
123 | unsigned char tmp[4]; | ||
124 | |||
125 | #define READ(x, len) if(fread(x, len, 1, infile) != 1) \ | ||
126 | { \ | ||
127 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ | ||
128 | fclose(infile); \ | ||
129 | return 4; \ | ||
130 | } | ||
131 | while(found < 0) | ||
132 | { | ||
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 | ||
140 | { | ||
141 | READ(&tmp[0], 4); | ||
142 | fseek(infile, le2int(tmp), SEEK_CUR); | ||
143 | } | ||
144 | free(filename); | ||
145 | } | ||
146 | |||
147 | fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); | ||
148 | |||
149 | if((outfile = fopen(argv[2], "wb+")) == NULL) | ||
150 | { | ||
151 | fclose(infile); | ||
152 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); | ||
153 | return 5; | ||
154 | } | ||
155 | |||
156 | #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ | ||
157 | { \ | ||
158 | fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ | ||
159 | fclose(outfile); \ | ||
160 | if(fw != NULL) \ | ||
161 | fclose(fw); \ | ||
162 | return 5; \ | ||
163 | } | ||
164 | |||
165 | unsigned char* buffer; | ||
166 | |||
167 | buffer = (unsigned char*)malloc(found); | ||
168 | fseek(infile, 0, SEEK_SET); | ||
169 | READ(buffer, found); | ||
170 | WRITE(buffer, found); | ||
171 | free(buffer); | ||
172 | |||
173 | if((fw = fopen(argv[3], "rb")) == NULL) | ||
174 | { | ||
175 | fclose(infile); | ||
176 | fclose(outfile); | ||
177 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); | ||
178 | } | ||
179 | |||
180 | int fw_filesize = _filesize(fw); | ||
181 | |||
182 | #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ | ||
183 | { \ | ||
184 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ | ||
185 | fclose(infile); \ | ||
186 | fclose(outfile); \ | ||
187 | return 6; \ | ||
188 | } | ||
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; | ||
197 | |||
198 | fseek(infile, found+1, SEEK_SET); | ||
199 | READ(&tmp, 4); | ||
200 | if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) | ||
201 | { | ||
202 | fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); | ||
203 | fclose(infile); | ||
204 | fclose(outfile); | ||
205 | return 7; | ||
206 | } | ||
207 | found = ftell(infile); | ||
208 | |||
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); | ||
215 | |||
216 | fflush(outfile); | ||
217 | fseek(outfile, 0x14, SEEK_SET); | ||
218 | WRITE(int2le(_filesize(outfile)), 4); | ||
219 | WRITE(int2le(checksum(outfile)), 4); | ||
220 | fclose(outfile); | ||
221 | |||
222 | fprintf(stderr, "[INFO] Done!\n"); | ||
223 | |||
224 | return 0; | ||
225 | } | ||