blob: 7237572fac4bd0847f3e68aab2f15f4efedb99dc [file] [log] [blame]
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +00001/***************************************************************************
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
Maurus Cuelenaere11826e92009-08-21 15:46:15 +000030#define tr(x) x /* Qt translation support */
31
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +000032/* From http://www.rockbox.org/wiki/ChinaChip */
33struct header
34{
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));
43
44static inline void int2le(unsigned char* addr, unsigned int val)
45{
46 addr[0] = val & 0xff;
47 addr[1] = (val >> 8) & 0xff;
48 addr[2] = (val >> 16) & 0xff;
49 addr[3] = (val >> 24) & 0xff;
50}
51
52static inline unsigned int le2int(unsigned char* buf)
53{
54 return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
55}
56
57static long int filesize(FILE* fd)
58{
59 long int len;
60 fseek(fd, 0, SEEK_END);
61 len = ftell(fd);
62 fseek(fd, 0, SEEK_SET);
63 return len;
64}
65
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)
77int 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)
82{
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;
90
91 fd = fopen(firmware, "rb");
92 if(!fd)
93 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +000094 ERR(tr("Can't open file %s!"), firmware);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +000095 goto err;
96 }
97 bd = fopen(bootloader, "rb");
98 if(!bd)
99 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000100 ERR(tr("Can't open file %s!"), bootloader);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000101 goto err;
102 }
103
104 bsize = filesize(bd);
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000105 INFO(tr("Bootloader size is %d bytes"), bsize);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000106 FCLOSE(bd);
107
108 fsize = filesize(fd);
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000109 INFO(tr("Firmware size is %d bytes"), fsize);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000110
111 buf = malloc(TOTAL_SIZE);
112 if(buf == NULL)
113 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000114 ERR(tr("Can't allocate %d bytes!"), fsize);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000115 goto err;
116 }
117 memset(buf, 0, TOTAL_SIZE);
118
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000119 INFO(tr("Reading %s into memory..."), firmware);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000120 if(fread(buf, fsize, 1, fd) != 1)
121 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000122 ERR(tr("Can't read file %s to memory!"), firmware);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000123 goto err;
124 }
125 FCLOSE(fd);
126
127 if(memcmp(buf, "WADF", 4))
128 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000129 ERR(tr("File %s isn't a valid ChinaChip firmware!"), firmware);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000130 goto err;
131 }
132
133 ccpmp_pos = -1, i = 0x40;
134 do
135 {
136 int filenamesize = le2int(&buf[i]);
137 i += sizeof(uint32_t);
138
139 if(!strncmp((char*) &buf[i], "ccpmp.bin", 9))
140 {
141 ccpmp_pos = i;
142 ccpmp_size = le2int(&buf[i + sizeof(uint8_t) + filenamesize]);
143 }
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);
148
149 if(i >= fsize)
150 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000151 ERR(tr("Couldn't find ccpmp.bin in %s!"), firmware);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000152 goto err;
153 }
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000154 INFO(tr("Found ccpmp.bin at %d bytes"), ccpmp_pos);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000155
156 if(ccpmp_backup)
157 {
Maurus Cuelenaere98f5c302009-09-14 12:29:34 +0000158 int ccpmp_data_pos = ccpmp_pos + 9;
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000159 bd = fopen(ccpmp_backup, "wb");
160 if(!bd)
161 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000162 ERR(tr("Can't open file %s!"), ccpmp_backup);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000163 goto err;
164 }
165
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000166 INFO(tr("Writing %d bytes to %s..."), ccpmp_size, ccpmp_backup);
Maurus Cuelenaere98f5c302009-09-14 12:29:34 +0000167 if(fwrite(&buf[ccpmp_data_pos], ccpmp_size, 1, bd) != 1)
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000168 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000169 ERR(tr("Can't write to file %s!"), ccpmp_backup);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000170 goto err;
171 }
172 FCLOSE(bd);
173 }
174
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000175 INFO(tr("Renaming it to ccpmp.old..."));
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000176 buf[ccpmp_pos + 6] = 'o';
177 buf[ccpmp_pos + 7] = 'l';
178 buf[ccpmp_pos + 8] = 'd';
179
180 bd = fopen(bootloader, "rb");
181 if(!bd)
182 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000183 ERR(tr("Can't open file %s!"), bootloader);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000184 goto err;
185 }
186
187 /* Also include path size */
188 ccpmp_pos -= sizeof(uint32_t);
189
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000190 INFO(tr("Making place for ccpmp.bin..."));
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000191 memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE],
192 &buf[ccpmp_pos], fsize - ccpmp_pos);
193
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000194 INFO(tr("Reading %s into memory..."), bootloader);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000195 if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE],
196 bsize, 1, bd) != 1)
197 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000198 ERR(tr("Can't read file %s to memory!"), bootloader);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000199 goto err;
200 }
201 FCLOSE(bd);
202
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000203 INFO(tr("Adding header to %s..."), bootloader);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000204 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 */
208
209 time(&cur_time);
210 time_info = localtime(&cur_time);
211 if(time_info == NULL)
212 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000213 ERR(tr("Can't obtain current time!"));
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000214 goto err;
215 }
216
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);
222
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000223 INFO(tr("Computing checksum..."));
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000224 for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4)
225 checksum += le2int(&buf[i]);
226
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000227 INFO(tr("Updating main header..."));
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000228 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);
231
232 od = fopen(output, "wb");
233 if(!od)
234 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000235 ERR(tr("Can't open file %s!"), output);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000236 goto err;
237 }
238
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000239 INFO(tr("Writing output to %s..."), output);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000240 if(fwrite(buf, TOTAL_SIZE, 1, od) != 1)
241 {
Maurus Cuelenaere11826e92009-08-21 15:46:15 +0000242 ERR(tr("Can't write to file %s!"), output);
Maurus Cuelenaeree8c71aa2009-08-16 20:39:00 +0000243 goto err;
244 }
245 fclose(od);
246 free(buf);
247
248 return 0;
249
250err:
251 if(buf)
252 free(buf);
253 if(fd)
254 fclose(fd);
255 if(bd)
256 fclose(bd);
257 if(od)
258 fclose(od);
259
260 return -1;
261}
262
263
264#ifdef STANDALONE
265
266#define VERSION "0.1"
267#define PRINT(fmt, ...) fprintf(stderr, fmt"\n", ##__VA_ARGS__)
268
269static void info(void* userdata, char* fmt, ...)
270{
271 (void)userdata;
272 va_list args;
273 va_start(args, fmt);
274 vfprintf(stderr, fmt, args);
275 va_end(args);
276}
277
278static void err(void* userdata, char* fmt, ...)
279{
280 (void)userdata;
281 va_list args;
282 va_start(args, fmt);
283 vfprintf(stderr, fmt, args);
284 va_end(args);
285}
286
287void usage(char* name)
288{
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.");
296}
297
298int main(int argc, char* argv[])
299{
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");
303
304 if(argc < 4)
305 {
306 usage(argv[0]);
307 return 1;
308 }
309
310 return chinachip_patch(argv[1], argv[2], argv[3], argc > 4 ? argv[4] : NULL,
311 &info, &err, NULL);
312}
313#endif