diff options
Diffstat (limited to 'utils/jz4740_tools/HXFreplace.c')
-rw-r--r-- | utils/jz4740_tools/HXFreplace.c | 484 |
1 files changed, 242 insertions, 242 deletions
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 @@ | |||
1 | /*************************************************************************** | 1 | /*************************************************************************** |
2 | * __________ __ ___. | 2 | * __________ __ ___. |
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
7 | * \/ \/ \/ \/ \/ | 7 | * \/ \/ \/ \/ \/ |
8 | * $Id$ | 8 | * $Id$ |
9 | * | 9 | * |
10 | * Copyright (C) 2008 by Maurus Cuelenaere | 10 | * Copyright (C) 2008 by Maurus Cuelenaere |
11 | * | 11 | * |
12 | * This program is free software; you can redistribute it and/or | 12 | * This program is free software; you can redistribute it and/or |
13 | * modify it under the terms of the GNU General Public License | 13 | * modify it under the terms of the GNU General Public License |
14 | * as published by the Free Software Foundation; either version 2 | 14 | * as published by the Free Software Foundation; either version 2 |
15 | * of the License, or (at your option) any later version. | 15 | * of the License, or (at your option) any later version. |
16 | * | 16 | * |
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
18 | * KIND, either express or implied. | 18 | * KIND, either express or implied. |
19 | * | 19 | * |
20 | ****************************************************************************/ | 20 | ****************************************************************************/ |
21 | 21 | ||
22 | #include <stdio.h> | 22 | #include <stdio.h> |
23 | #include <stdlib.h> | 23 | #include <stdlib.h> |
24 | #include <string.h> | 24 | #include <string.h> |
25 | #include <sys/types.h> | 25 | #include <sys/types.h> |
26 | #include <fcntl.h> | 26 | #include <fcntl.h> |
27 | #include <unistd.h> | 27 | #include <unistd.h> |
28 | #include <sys/stat.h> | 28 | #include <sys/stat.h> |
29 | #include <stdbool.h> | 29 | #include <stdbool.h> |
30 | #include <dirent.h> | 30 | #include <dirent.h> |
31 | 31 | ||
32 | #define VERSION "0.1" | 32 | #define VERSION "0.1" |
33 | 33 | ||
34 | static unsigned char* int2le(unsigned int val) | 34 | static unsigned char* int2le(unsigned int val) |
35 | { | 35 | { |
36 | static unsigned char addr[4]; | 36 | static unsigned char addr[4]; |
37 | addr[0] = val & 0xff; | 37 | addr[0] = val & 0xff; |
38 | addr[1] = (val >> 8) & 0xff; | 38 | addr[1] = (val >> 8) & 0xff; |
39 | addr[2] = (val >> 16) & 0xff; | 39 | addr[2] = (val >> 16) & 0xff; |
40 | addr[3] = (val >> 24) & 0xff; | 40 | addr[3] = (val >> 24) & 0xff; |
41 | return addr; | 41 | return addr; |
42 | } | 42 | } |
43 | 43 | ||
44 | static unsigned int le2int(unsigned char* buf) | 44 | static unsigned int le2int(unsigned char* buf) |
45 | { | 45 | { |
46 | unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; | 46 | unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
47 | 47 | ||
48 | return res; | 48 | return res; |
49 | } | 49 | } |
50 | 50 | ||
51 | unsigned int _filesize(FILE* fd) | 51 | unsigned int _filesize(FILE* fd) |
52 | { | 52 | { |
53 | unsigned int tmp, oldpos; | 53 | unsigned int tmp, oldpos; |
54 | oldpos = ftell(fd); | 54 | oldpos = ftell(fd); |
55 | fseek(fd, 0, SEEK_END); | 55 | fseek(fd, 0, SEEK_END); |
56 | tmp = ftell(fd); | 56 | tmp = ftell(fd); |
57 | fseek(fd, oldpos, SEEK_SET); | 57 | fseek(fd, oldpos, SEEK_SET); |
58 | return tmp; | 58 | return tmp; |
59 | } | 59 | } |
60 | 60 | ||
61 | static void print_usage(void) | 61 | static void print_usage(void) |
62 | { | 62 | { |
63 | #ifdef _WIN32 | 63 | #ifdef _WIN32 |
64 | fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); | 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"); | 65 | fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n"); |
66 | #else | 66 | #else |
67 | fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); | 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"); | 68 | fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n"); |
69 | #endif | 69 | #endif |
70 | } | 70 | } |
71 | 71 | ||
72 | static int checksum(FILE *file) | 72 | static int checksum(FILE *file) |
73 | { | 73 | { |
74 | int oldpos = ftell(file); | 74 | int oldpos = ftell(file); |
75 | int ret=0, i, filesize = _filesize(file)-0x40; | 75 | int ret=0, i, filesize = _filesize(file)-0x40; |
76 | unsigned char *buf; | 76 | unsigned char *buf; |
77 | 77 | ||
78 | buf = (unsigned char*)malloc(filesize); | 78 | buf = (unsigned char*)malloc(filesize); |
79 | 79 | ||
80 | if(buf == NULL) | 80 | if(buf == NULL) |
81 | { | 81 | { |
82 | fseek(file, oldpos, SEEK_SET); | 82 | fseek(file, oldpos, SEEK_SET); |
83 | fprintf(stderr, "[ERR] Error while allocating memory\n"); | 83 | fprintf(stderr, "[ERR] Error while allocating memory\n"); |
84 | return 0; | 84 | return 0; |
85 | } | 85 | } |
86 | 86 | ||
87 | fseek(file, 0x40, SEEK_SET); | 87 | fseek(file, 0x40, SEEK_SET); |
88 | if(fread(buf, filesize, 1, file) != 1) | 88 | if(fread(buf, filesize, 1, file) != 1) |
89 | { | 89 | { |
90 | free(buf); | 90 | free(buf); |
91 | fseek(file, oldpos, SEEK_SET); | 91 | fseek(file, oldpos, SEEK_SET); |
92 | fprintf(stderr, "[ERR] Error while reading from file\n"); | 92 | fprintf(stderr, "[ERR] Error while reading from file\n"); |
93 | return 0; | 93 | return 0; |
94 | } | 94 | } |
95 | 95 | ||
96 | fprintf(stderr, "[INFO] Computing checksum..."); | 96 | fprintf(stderr, "[INFO] Computing checksum..."); |
97 | 97 | ||
98 | for(i = 0; i < filesize; i+=4) | 98 | for(i = 0; i < filesize; i+=4) |
99 | ret += le2int(&buf[i]); | 99 | ret += le2int(&buf[i]); |
100 | 100 | ||
101 | free(buf); | 101 | free(buf); |
102 | fseek(file, oldpos, SEEK_SET); | 102 | fseek(file, oldpos, SEEK_SET); |
103 | 103 | ||
104 | fprintf(stderr, " Done!\n"); | 104 | fprintf(stderr, " Done!\n"); |
105 | return ret; | 105 | return ret; |
106 | } | 106 | } |
107 | 107 | ||
108 | int main(int argc, char *argv[]) | 108 | int main(int argc, char *argv[]) |
109 | { | 109 | { |
110 | FILE *infile, *outfile, *fw; | 110 | FILE *infile, *outfile, *fw; |
111 | 111 | ||
112 | fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); | 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"); | 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"); | 114 | fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); |
115 | 115 | ||
116 | if(argc != 4) | 116 | if(argc != 4) |
117 | { | 117 | { |
118 | print_usage(); | 118 | print_usage(); |
119 | return 1; | 119 | return 1; |
120 | } | 120 | } |
121 | 121 | ||
122 | if((infile = fopen(argv[1], "rb")) == NULL) | 122 | if((infile = fopen(argv[1], "rb")) == NULL) |
123 | { | 123 | { |
124 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); | 124 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); |
125 | return 2; | 125 | return 2; |
126 | } | 126 | } |
127 | 127 | ||
128 | if(fseek(infile, 0x40, SEEK_SET) != 0) | 128 | if(fseek(infile, 0x40, SEEK_SET) != 0) |
129 | { | 129 | { |
130 | fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); | 130 | fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); |
131 | fclose(infile); | 131 | fclose(infile); |
132 | return 3; | 132 | return 3; |
133 | } | 133 | } |
134 | 134 | ||
135 | fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); | 135 | fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); |
136 | 136 | ||
137 | int found = -1; | 137 | int found = -1; |
138 | int filenamesize; | 138 | int filenamesize; |
139 | char *filename; | 139 | char *filename; |
140 | unsigned char tmp[4]; | 140 | unsigned char tmp[4]; |
141 | 141 | ||
142 | #define READ(x, len) if(fread(x, len, 1, infile) != 1) \ | 142 | #define READ(x, len) if(fread(x, len, 1, infile) != 1) \ |
143 | { \ | 143 | { \ |
144 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ | 144 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ |
145 | fclose(infile); \ | 145 | fclose(infile); \ |
146 | return 4; \ | 146 | return 4; \ |
147 | } | 147 | } |
148 | while(found < 0) | 148 | while(found < 0) |
149 | { | 149 | { |
150 | READ(&tmp[0], 4); | 150 | READ(&tmp[0], 4); |
151 | filenamesize = le2int(tmp); | 151 | filenamesize = le2int(tmp); |
152 | filename = (char*)malloc(filenamesize); | 152 | filename = (char*)malloc(filenamesize); |
153 | READ(filename, filenamesize); | 153 | READ(filename, filenamesize); |
154 | if(strcmp(filename, "ccpmp.bin") == 0) | 154 | if(strcmp(filename, "ccpmp.bin") == 0) |
155 | found = ftell(infile); | 155 | found = ftell(infile); |
156 | else | 156 | else |
157 | { | 157 | { |
158 | READ(&tmp[0], 4); | 158 | READ(&tmp[0], 4); |
159 | fseek(infile, le2int(tmp), SEEK_CUR); | 159 | fseek(infile, le2int(tmp), SEEK_CUR); |
160 | } | 160 | } |
161 | free(filename); | 161 | free(filename); |
162 | } | 162 | } |
163 | 163 | ||
164 | fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); | 164 | fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); |
165 | 165 | ||
166 | if((outfile = fopen(argv[2], "wb+")) == NULL) | 166 | if((outfile = fopen(argv[2], "wb+")) == NULL) |
167 | { | 167 | { |
168 | fclose(infile); | 168 | fclose(infile); |
169 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); | 169 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); |
170 | return 5; | 170 | return 5; |
171 | } | 171 | } |
172 | 172 | ||
173 | #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ | 173 | #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ |
174 | { \ | 174 | { \ |
175 | fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ | 175 | fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ |
176 | fclose(outfile); \ | 176 | fclose(outfile); \ |
177 | if(fw != NULL) \ | 177 | if(fw != NULL) \ |
178 | fclose(fw); \ | 178 | fclose(fw); \ |
179 | return 5; \ | 179 | return 5; \ |
180 | } | 180 | } |
181 | 181 | ||
182 | unsigned char* buffer; | 182 | unsigned char* buffer; |
183 | 183 | ||
184 | buffer = (unsigned char*)malloc(found); | 184 | buffer = (unsigned char*)malloc(found); |
185 | fseek(infile, 0, SEEK_SET); | 185 | fseek(infile, 0, SEEK_SET); |
186 | READ(buffer, found); | 186 | READ(buffer, found); |
187 | WRITE(buffer, found); | 187 | WRITE(buffer, found); |
188 | free(buffer); | 188 | free(buffer); |
189 | 189 | ||
190 | if((fw = fopen(argv[3], "rb")) == NULL) | 190 | if((fw = fopen(argv[3], "rb")) == NULL) |
191 | { | 191 | { |
192 | fclose(infile); | 192 | fclose(infile); |
193 | fclose(outfile); | 193 | fclose(outfile); |
194 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); | 194 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); |
195 | } | 195 | } |
196 | 196 | ||
197 | int fw_filesize = _filesize(fw); | 197 | int fw_filesize = _filesize(fw); |
198 | 198 | ||
199 | #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ | 199 | #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ |
200 | { \ | 200 | { \ |
201 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ | 201 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ |
202 | fclose(infile); \ | 202 | fclose(infile); \ |
203 | fclose(outfile); \ | 203 | fclose(outfile); \ |
204 | return 6; \ | 204 | return 6; \ |
205 | } | 205 | } |
206 | buffer = (unsigned char*)malloc(fw_filesize); | 206 | buffer = (unsigned char*)malloc(fw_filesize); |
207 | READ2(buffer, fw_filesize); | 207 | READ2(buffer, fw_filesize); |
208 | fputc(0x20, outfile); /* Padding */ | 208 | fputc(0x20, outfile); /* Padding */ |
209 | WRITE(int2le(fw_filesize), 4); | 209 | WRITE(int2le(fw_filesize), 4); |
210 | WRITE(buffer, fw_filesize); | 210 | WRITE(buffer, fw_filesize); |
211 | free(buffer); | 211 | free(buffer); |
212 | fclose(fw); | 212 | fclose(fw); |
213 | fw = NULL; | 213 | fw = NULL; |
214 | 214 | ||
215 | fseek(infile, found+1, SEEK_SET); | 215 | fseek(infile, found+1, SEEK_SET); |
216 | READ(&tmp, 4); | 216 | READ(&tmp, 4); |
217 | if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) | 217 | if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) |
218 | { | 218 | { |
219 | fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); | 219 | fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); |
220 | fclose(infile); | 220 | fclose(infile); |
221 | fclose(outfile); | 221 | fclose(outfile); |
222 | return 7; | 222 | return 7; |
223 | } | 223 | } |
224 | found = ftell(infile); | 224 | found = ftell(infile); |
225 | 225 | ||
226 | int other_size = _filesize(infile) - found; | 226 | int other_size = _filesize(infile) - found; |
227 | buffer = (unsigned char*)malloc(other_size); | 227 | buffer = (unsigned char*)malloc(other_size); |
228 | READ(buffer, other_size); | 228 | READ(buffer, other_size); |
229 | WRITE(buffer, other_size); | 229 | WRITE(buffer, other_size); |
230 | free(buffer); | 230 | free(buffer); |
231 | fclose(infile); | 231 | fclose(infile); |
232 | 232 | ||
233 | fflush(outfile); | 233 | fflush(outfile); |
234 | fseek(outfile, 0x14, SEEK_SET); | 234 | fseek(outfile, 0x14, SEEK_SET); |
235 | WRITE(int2le(_filesize(outfile)), 4); | 235 | WRITE(int2le(_filesize(outfile)), 4); |
236 | WRITE(int2le(checksum(outfile)), 4); | 236 | WRITE(int2le(checksum(outfile)), 4); |
237 | fclose(outfile); | 237 | fclose(outfile); |
238 | 238 | ||
239 | fprintf(stderr, "[INFO] Done!\n"); | 239 | fprintf(stderr, "[INFO] Done!\n"); |
240 | 240 | ||
241 | return 0; | 241 | return 0; |
242 | } | 242 | } |