summaryrefslogtreecommitdiff
path: root/rbutil/chinachippatcher
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/chinachippatcher')
-rw-r--r--rbutil/chinachippatcher/Makefile10
-rw-r--r--rbutil/chinachippatcher/chinachip.c310
-rw-r--r--rbutil/chinachippatcher/chinachip.h39
3 files changed, 359 insertions, 0 deletions
diff --git a/rbutil/chinachippatcher/Makefile b/rbutil/chinachippatcher/Makefile
new file mode 100644
index 0000000000..e19052a006
--- /dev/null
+++ b/rbutil/chinachippatcher/Makefile
@@ -0,0 +1,10 @@
1CFLAGS=-g -Wall -DSTANDALONE
2CC=gcc
3
4all: chinachip
5
6chinachip: chinachip.c
7 $(CC) $(CFLAGS) -o $@ $^
8
9clean:
10 rm -f chinachip
diff --git a/rbutil/chinachippatcher/chinachip.c b/rbutil/chinachippatcher/chinachip.c
new file mode 100644
index 0000000000..e7d4095b38
--- /dev/null
+++ b/rbutil/chinachippatcher/chinachip.c
@@ -0,0 +1,310 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2009 by Maurus Cuelenaere
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include <stdio.h>
22#include <stdarg.h>
23#include <stddef.h>
24#include <stdlib.h>
25#include <stdint.h>
26#include <string.h>
27#include <time.h>
28#include "chinachip.h"
29
30/* From http://www.rockbox.org/wiki/ChinaChip */
31struct header
32{
33 uint32_t signature; /* WADF */
34 uint32_t unk;
35 int8_t timestamp[12]; /* 200805081100 */
36 uint32_t size;
37 uint32_t checksum;
38 uint32_t unk2;
39 int8_t identifier[32]; /* Chinachip PMP firmware V1.0 */
40} __attribute__ ((packed));
41
42static inline void int2le(unsigned char* addr, unsigned int val)
43{
44 addr[0] = val & 0xff;
45 addr[1] = (val >> 8) & 0xff;
46 addr[2] = (val >> 16) & 0xff;
47 addr[3] = (val >> 24) & 0xff;
48}
49
50static inline unsigned int le2int(unsigned char* buf)
51{
52 return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
53}
54
55static long int filesize(FILE* fd)
56{
57 long int len;
58 fseek(fd, 0, SEEK_END);
59 len = ftell(fd);
60 fseek(fd, 0, SEEK_SET);
61 return len;
62}
63
64#ifdef STANDALONE
65#define ERR(fmt, ...) err(userdata, "[ERR] "fmt"\n", ##__VA_ARGS__)
66#define INFO(fmt, ...) info(userdata, "[INFO] "fmt"\n", ##__VA_ARGS__)
67#define tr(x) x
68#else
69#define ERR(fmt, ...) err(userdata, fmt, ##__VA_ARGS__)
70#define INFO(fmt, ...) info(userdata, fmt, ##__VA_ARGS__)
71#endif
72#define FCLOSE(fd) fclose(fd); fd = NULL;
73#define CCPMPBIN_HEADER_SIZE (sizeof(uint32_t)*2 + sizeof(uint8_t) + 9)
74#define TOTAL_SIZE (fsize + CCPMPBIN_HEADER_SIZE + bsize)
75int chinachip_patch(const char* firmware, const char* bootloader,
76 const char* output, const char* ccpmp_backup,
77 void (*info)(void*, char*, ...),
78 void (*err)(void*, char*, ...),
79 void* userdata)
80{
81 char header_time[13];
82 time_t cur_time;
83 struct tm* time_info;
84 unsigned char* buf = NULL;
85 FILE *fd = NULL, *bd = NULL, *od = NULL;
86 unsigned int ccpmp_size = 0, i, fsize, bsize;
87 signed int checksum = 0, ccpmp_pos;
88
89 fd = fopen(firmware, "rb");
90 if(!fd)
91 {
92 ERR("Can't open file %s!", firmware);
93 goto err;
94 }
95 bd = fopen(bootloader, "rb");
96 if(!bd)
97 {
98 ERR("Can't open file %s!", bootloader);
99 goto err;
100 }
101
102 bsize = filesize(bd);
103 INFO("Bootloader size is %d bytes", bsize);
104 FCLOSE(bd);
105
106 fsize = filesize(fd);
107 INFO("Firmware size is %d bytes", fsize);
108
109 buf = malloc(TOTAL_SIZE);
110 if(buf == NULL)
111 {
112 ERR("Can't allocate %d bytes!", fsize);
113 goto err;
114 }
115 memset(buf, 0, TOTAL_SIZE);
116
117 INFO("Reading %s into memory...", firmware);
118 if(fread(buf, fsize, 1, fd) != 1)
119 {
120 ERR("Can't read file %s to memory!", firmware);
121 goto err;
122 }
123 FCLOSE(fd);
124
125 if(memcmp(buf, "WADF", 4))
126 {
127 ERR("File %s isn't a valid ChinaChip firmware!", firmware);
128 goto err;
129 }
130
131 ccpmp_pos = -1, i = 0x40;
132 do
133 {
134 int filenamesize = le2int(&buf[i]);
135 i += sizeof(uint32_t);
136
137 if(!strncmp((char*) &buf[i], "ccpmp.bin", 9))
138 {
139 ccpmp_pos = i;
140 ccpmp_size = le2int(&buf[i + sizeof(uint8_t) + filenamesize]);
141 }
142 else
143 i += filenamesize + le2int(&buf[i + sizeof(uint8_t) + filenamesize])
144 + sizeof(uint32_t) + sizeof(uint8_t);
145 } while(ccpmp_pos < 0 && i < fsize);
146
147 if(i >= fsize)
148 {
149 ERR("Couldn't find ccpmp.bin in %s!", firmware);
150 goto err;
151 }
152 INFO("Found ccpmp.bin at %d bytes", ccpmp_pos);
153
154 if(ccpmp_backup)
155 {
156 bd = fopen(ccpmp_backup, "wb");
157 if(!bd)
158 {
159 ERR("Can't open file %s!", ccpmp_backup);
160 goto err;
161 }
162
163 INFO("Writing %d bytes to %s...", ccpmp_size, ccpmp_backup);
164 if(fwrite(&buf[ccpmp_pos], ccpmp_size, 1, bd) != 1)
165 {
166 ERR("Can't write to file %s!", ccpmp_backup);
167 goto err;
168 }
169 FCLOSE(bd);
170 }
171
172 INFO("Renaming it to ccpmp.old...");
173 buf[ccpmp_pos + 6] = 'o';
174 buf[ccpmp_pos + 7] = 'l';
175 buf[ccpmp_pos + 8] = 'd';
176
177 bd = fopen(bootloader, "rb");
178 if(!bd)
179 {
180 ERR("Can't open file %s!", bootloader);
181 goto err;
182 }
183
184 /* Also include path size */
185 ccpmp_pos -= sizeof(uint32_t);
186
187 INFO("Making place for ccpmp.bin...");
188 memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE],
189 &buf[ccpmp_pos], fsize - ccpmp_pos);
190
191 INFO("Reading %s into memory...", bootloader);
192 if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE],
193 bsize, 1, bd) != 1)
194 {
195 ERR("Can't read file %s to memory!", bootloader);
196 goto err;
197 }
198 FCLOSE(bd);
199
200 INFO("Adding header to %s...", bootloader);
201 int2le(&buf[ccpmp_pos ], 9); /* Pathname Size */
202 memcpy(&buf[ccpmp_pos + 4 ], "ccpmp.bin", 9); /* Pathname */
203 memset(&buf[ccpmp_pos + 4 + 9 ], 0x20, sizeof(uint8_t)); /* File Type */
204 int2le(&buf[ccpmp_pos + 4 + 9 + 1], bsize); /* File Size */
205
206 time(&cur_time);
207 time_info = localtime(&cur_time);
208 if(time_info == NULL)
209 {
210 ERR("Can't obtain current time!");
211 goto err;
212 }
213
214 snprintf(header_time, 13, "%04d%02d%02d%02d%02d", time_info->tm_year + 1900,
215 time_info->tm_mon,
216 time_info->tm_mday,
217 time_info->tm_hour,
218 time_info->tm_min);
219
220 INFO("Computing checksum...");
221 for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4)
222 checksum += le2int(&buf[i]);
223
224 INFO("Updating main header...");
225 memcpy(&buf[offsetof(struct header, timestamp)], header_time, 12);
226 int2le(&buf[offsetof(struct header, size) ], TOTAL_SIZE);
227 int2le(&buf[offsetof(struct header, checksum) ], checksum);
228
229 od = fopen(output, "wb");
230 if(!od)
231 {
232 ERR("Can't open file %s!", output);
233 goto err;
234 }
235
236 INFO("Writing output to %s...", output);
237 if(fwrite(buf, TOTAL_SIZE, 1, od) != 1)
238 {
239 ERR("Can't write to file %s!", output);
240 goto err;
241 }
242 fclose(od);
243 free(buf);
244
245 return 0;
246
247err:
248 if(buf)
249 free(buf);
250 if(fd)
251 fclose(fd);
252 if(bd)
253 fclose(bd);
254 if(od)
255 fclose(od);
256
257 return -1;
258}
259
260
261#ifdef STANDALONE
262
263#define VERSION "0.1"
264#define PRINT(fmt, ...) fprintf(stderr, fmt"\n", ##__VA_ARGS__)
265
266static void info(void* userdata, char* fmt, ...)
267{
268 (void)userdata;
269 va_list args;
270 va_start(args, fmt);
271 vfprintf(stderr, fmt, args);
272 va_end(args);
273}
274
275static void err(void* userdata, char* fmt, ...)
276{
277 (void)userdata;
278 va_list args;
279 va_start(args, fmt);
280 vfprintf(stderr, fmt, args);
281 va_end(args);
282}
283
284void usage(char* name)
285{
286 PRINT("Usage:");
287 PRINT(" %s <firmware> <bootloader> <firmware_output> [backup]", name);
288 PRINT("\nExample:");
289 PRINT(" %s VX747.HXF bootloader.bin output.HXF ccpmp.bak", name);
290 PRINT(" This will copy ccpmp.bin in VX747.HXF as ccpmp.old and replace it"
291 " with bootloader.bin, the output will get written to output.HXF."
292 " The old ccpmp.bin will get written to ccpmp.bak.");
293}
294
295int main(int argc, char* argv[])
296{
297 PRINT("ChinaChipPatcher v" VERSION " - (C) Maurus Cuelenaere 2009");
298 PRINT("This is free software; see the source for copying conditions. There is NO");
299 PRINT("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
300
301 if(argc < 4)
302 {
303 usage(argv[0]);
304 return 1;
305 }
306
307 return chinachip_patch(argv[1], argv[2], argv[3], argc > 4 ? argv[4] : NULL,
308 &info, &err, NULL);
309}
310#endif
diff --git a/rbutil/chinachippatcher/chinachip.h b/rbutil/chinachippatcher/chinachip.h
new file mode 100644
index 0000000000..2f8ba9e18a
--- /dev/null
+++ b/rbutil/chinachippatcher/chinachip.h
@@ -0,0 +1,39 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2009 by Maurus Cuelenaere
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef __INCLUDE_CHINACHIP_H_
23#define __INCLUDE_CHINACHIP_H_
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29int chinachip_patch(const char* firmware, const char* bootloader,
30 const char* output, const char* ccpmp_backup,
31 void (*info)(void*, char*, ...),
32 void (*err)(void*, char*, ...),
33 void* userdata);
34
35#ifdef __cplusplus
36}
37#endif
38
39#endif /* __INCLUDE_CHINACHIP_H_ */