Add a patch file in libwmapro to make it easier to add the library and the decoder...
[kugel-rb.git] / rbutil / chinachippatcher / chinachip.c
blob7237572fac4bd0847f3e68aab2f15f4efedb99dc
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 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 ****************************************************************************/
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"
30 #define tr(x) x /* Qt translation support */
32 /* From http://www.rockbox.org/wiki/ChinaChip */
33 struct header
35 uint32_t signature; /* WADF */
36 uint32_t unk;
37 int8_t timestamp[12]; /* 200805081100 */
38 uint32_t size;
39 uint32_t checksum;
40 uint32_t unk2;
41 int8_t identifier[32]; /* Chinachip PMP firmware V1.0 */
42 } __attribute__ ((packed));
44 static inline void int2le(unsigned char* addr, unsigned int val)
46 addr[0] = val & 0xff;
47 addr[1] = (val >> 8) & 0xff;
48 addr[2] = (val >> 16) & 0xff;
49 addr[3] = (val >> 24) & 0xff;
52 static inline unsigned int le2int(unsigned char* buf)
54 return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
57 static long int filesize(FILE* fd)
59 long int len;
60 fseek(fd, 0, SEEK_END);
61 len = ftell(fd);
62 fseek(fd, 0, SEEK_SET);
63 return len;
66 #ifdef STANDALONE
67 #define ERR(fmt, ...) err(userdata, "[ERR] "fmt"\n", ##__VA_ARGS__)
68 #define INFO(fmt, ...) info(userdata, "[INFO] "fmt"\n", ##__VA_ARGS__)
69 #define tr(x) x
70 #else
71 #define ERR(fmt, ...) err(userdata, fmt, ##__VA_ARGS__)
72 #define INFO(fmt, ...) info(userdata, fmt, ##__VA_ARGS__)
73 #endif
74 #define FCLOSE(fd) fclose(fd); fd = NULL;
75 #define CCPMPBIN_HEADER_SIZE (sizeof(uint32_t)*2 + sizeof(uint8_t) + 9)
76 #define TOTAL_SIZE (fsize + CCPMPBIN_HEADER_SIZE + bsize)
77 int chinachip_patch(const char* firmware, const char* bootloader,
78 const char* output, const char* ccpmp_backup,
79 void (*info)(void*, char*, ...),
80 void (*err)(void*, char*, ...),
81 void* userdata)
83 char header_time[13];
84 time_t cur_time;
85 struct tm* time_info;
86 unsigned char* buf = NULL;
87 FILE *fd = NULL, *bd = NULL, *od = NULL;
88 unsigned int ccpmp_size = 0, i, fsize, bsize;
89 signed int checksum = 0, ccpmp_pos;
91 fd = fopen(firmware, "rb");
92 if(!fd)
94 ERR(tr("Can't open file %s!"), firmware);
95 goto err;
97 bd = fopen(bootloader, "rb");
98 if(!bd)
100 ERR(tr("Can't open file %s!"), bootloader);
101 goto err;
104 bsize = filesize(bd);
105 INFO(tr("Bootloader size is %d bytes"), bsize);
106 FCLOSE(bd);
108 fsize = filesize(fd);
109 INFO(tr("Firmware size is %d bytes"), fsize);
111 buf = malloc(TOTAL_SIZE);
112 if(buf == NULL)
114 ERR(tr("Can't allocate %d bytes!"), fsize);
115 goto err;
117 memset(buf, 0, TOTAL_SIZE);
119 INFO(tr("Reading %s into memory..."), firmware);
120 if(fread(buf, fsize, 1, fd) != 1)
122 ERR(tr("Can't read file %s to memory!"), firmware);
123 goto err;
125 FCLOSE(fd);
127 if(memcmp(buf, "WADF", 4))
129 ERR(tr("File %s isn't a valid ChinaChip firmware!"), firmware);
130 goto err;
133 ccpmp_pos = -1, i = 0x40;
136 int filenamesize = le2int(&buf[i]);
137 i += sizeof(uint32_t);
139 if(!strncmp((char*) &buf[i], "ccpmp.bin", 9))
141 ccpmp_pos = i;
142 ccpmp_size = le2int(&buf[i + sizeof(uint8_t) + filenamesize]);
144 else
145 i += filenamesize + le2int(&buf[i + sizeof(uint8_t) + filenamesize])
146 + sizeof(uint32_t) + sizeof(uint8_t);
147 } while(ccpmp_pos < 0 && i < fsize);
149 if(i >= fsize)
151 ERR(tr("Couldn't find ccpmp.bin in %s!"), firmware);
152 goto err;
154 INFO(tr("Found ccpmp.bin at %d bytes"), ccpmp_pos);
156 if(ccpmp_backup)
158 int ccpmp_data_pos = ccpmp_pos + 9;
159 bd = fopen(ccpmp_backup, "wb");
160 if(!bd)
162 ERR(tr("Can't open file %s!"), ccpmp_backup);
163 goto err;
166 INFO(tr("Writing %d bytes to %s..."), ccpmp_size, ccpmp_backup);
167 if(fwrite(&buf[ccpmp_data_pos], ccpmp_size, 1, bd) != 1)
169 ERR(tr("Can't write to file %s!"), ccpmp_backup);
170 goto err;
172 FCLOSE(bd);
175 INFO(tr("Renaming it to ccpmp.old..."));
176 buf[ccpmp_pos + 6] = 'o';
177 buf[ccpmp_pos + 7] = 'l';
178 buf[ccpmp_pos + 8] = 'd';
180 bd = fopen(bootloader, "rb");
181 if(!bd)
183 ERR(tr("Can't open file %s!"), bootloader);
184 goto err;
187 /* Also include path size */
188 ccpmp_pos -= sizeof(uint32_t);
190 INFO(tr("Making place for ccpmp.bin..."));
191 memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE],
192 &buf[ccpmp_pos], fsize - ccpmp_pos);
194 INFO(tr("Reading %s into memory..."), bootloader);
195 if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE],
196 bsize, 1, bd) != 1)
198 ERR(tr("Can't read file %s to memory!"), bootloader);
199 goto err;
201 FCLOSE(bd);
203 INFO(tr("Adding header to %s..."), bootloader);
204 int2le(&buf[ccpmp_pos ], 9); /* Pathname Size */
205 memcpy(&buf[ccpmp_pos + 4 ], "ccpmp.bin", 9); /* Pathname */
206 memset(&buf[ccpmp_pos + 4 + 9 ], 0x20, sizeof(uint8_t)); /* File Type */
207 int2le(&buf[ccpmp_pos + 4 + 9 + 1], bsize); /* File Size */
209 time(&cur_time);
210 time_info = localtime(&cur_time);
211 if(time_info == NULL)
213 ERR(tr("Can't obtain current time!"));
214 goto err;
217 snprintf(header_time, 13, "%04d%02d%02d%02d%02d", time_info->tm_year + 1900,
218 time_info->tm_mon,
219 time_info->tm_mday,
220 time_info->tm_hour,
221 time_info->tm_min);
223 INFO(tr("Computing checksum..."));
224 for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4)
225 checksum += le2int(&buf[i]);
227 INFO(tr("Updating main header..."));
228 memcpy(&buf[offsetof(struct header, timestamp)], header_time, 12);
229 int2le(&buf[offsetof(struct header, size) ], TOTAL_SIZE);
230 int2le(&buf[offsetof(struct header, checksum) ], checksum);
232 od = fopen(output, "wb");
233 if(!od)
235 ERR(tr("Can't open file %s!"), output);
236 goto err;
239 INFO(tr("Writing output to %s..."), output);
240 if(fwrite(buf, TOTAL_SIZE, 1, od) != 1)
242 ERR(tr("Can't write to file %s!"), output);
243 goto err;
245 fclose(od);
246 free(buf);
248 return 0;
250 err:
251 if(buf)
252 free(buf);
253 if(fd)
254 fclose(fd);
255 if(bd)
256 fclose(bd);
257 if(od)
258 fclose(od);
260 return -1;
264 #ifdef STANDALONE
266 #define VERSION "0.1"
267 #define PRINT(fmt, ...) fprintf(stderr, fmt"\n", ##__VA_ARGS__)
269 static void info(void* userdata, char* fmt, ...)
271 (void)userdata;
272 va_list args;
273 va_start(args, fmt);
274 vfprintf(stderr, fmt, args);
275 va_end(args);
278 static void err(void* userdata, char* fmt, ...)
280 (void)userdata;
281 va_list args;
282 va_start(args, fmt);
283 vfprintf(stderr, fmt, args);
284 va_end(args);
287 void usage(char* name)
289 PRINT("Usage:");
290 PRINT(" %s <firmware> <bootloader> <firmware_output> [backup]", name);
291 PRINT("\nExample:");
292 PRINT(" %s VX747.HXF bootloader.bin output.HXF ccpmp.bak", name);
293 PRINT(" This will copy ccpmp.bin in VX747.HXF as ccpmp.old and replace it"
294 " with bootloader.bin, the output will get written to output.HXF."
295 " The old ccpmp.bin will get written to ccpmp.bak.");
298 int main(int argc, char* argv[])
300 PRINT("ChinaChipPatcher v" VERSION " - (C) Maurus Cuelenaere 2009");
301 PRINT("This is free software; see the source for copying conditions. There is NO");
302 PRINT("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
304 if(argc < 4)
306 usage(argv[0]);
307 return 1;
310 return chinachip_patch(argv[1], argv[2], argv[3], argc > 4 ? argv[4] : NULL,
311 &info, &err, NULL);
313 #endif