summaryrefslogtreecommitdiffstats
path: root/rbutil/chinachippatcher/chinachip.c
blob: cafb1d189b64934eca37116cca03724af7fc2fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 by Maurus Cuelenaere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "chinachip.h"

#define tr(x) x /* Qt translation support */

/* From http://www.rockbox.org/wiki/ChinaChip */
struct header
{
    uint32_t signature;      /* WADF */
    uint32_t unk;
    int8_t   timestamp[12];  /* 200805081100 */
    uint32_t size;
    uint32_t checksum;
    uint32_t unk2;
    int8_t   identifier[32]; /* Chinachip PMP firmware V1.0 */
} __attribute__ ((packed));

static inline void int2le(unsigned char* addr, unsigned int val)
{
    addr[0] = val & 0xff;
    addr[1] = (val >> 8) & 0xff;
    addr[2] = (val >> 16) & 0xff;
    addr[3] = (val >> 24) & 0xff;
}

static inline unsigned int le2int(unsigned char* buf)
{
   return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
}

static long int filesize(FILE* fd)
{
    long int len;
    fseek(fd, 0, SEEK_END);
    len = ftell(fd);
    fseek(fd, 0, SEEK_SET);
    return len;
}

#ifdef STANDALONE
#define ERR(fmt, ...)   err(userdata, "[ERR]  "fmt"\n", ##__VA_ARGS__)
#define INFO(fmt, ...)  info(userdata, "[INFO] "fmt"\n", ##__VA_ARGS__)
#define tr(x) x
#else
#define ERR(fmt, ...)   err(userdata, fmt, ##__VA_ARGS__)
#define INFO(fmt, ...)  info(userdata, fmt, ##__VA_ARGS__)
#endif
#define FCLOSE(fd) fclose(fd); fd = NULL;
#define CCPMPBIN_HEADER_SIZE (sizeof(uint32_t)*2 + sizeof(uint8_t) + 9)
#define TOTAL_SIZE (fsize + CCPMPBIN_HEADER_SIZE + bsize)
int chinachip_patch(const char* firmware, const char* bootloader,
                    const char* output, const char* ccpmp_backup,
                    void (*info)(void*, char*, ...),
                    void (*err)(void*, char*, ...),
                    void* userdata)
{
    char header_time[13];
    time_t cur_time;
    struct tm* time_info;
    unsigned char* buf = NULL;
    FILE *fd = NULL, *bd = NULL, *od = NULL;
    unsigned int ccpmp_size = 0, i, fsize, bsize;
    signed int checksum = 0, ccpmp_pos;

    fd = fopen(firmware, "rb");
    if(!fd)
    {
        ERR(tr("Can't open file %s!"), firmware);
        goto err;
    }
    bd = fopen(bootloader, "rb");
    if(!bd)
    {
        ERR(tr("Can't open file %s!"), bootloader);
        goto err;
    }

    bsize = filesize(bd);
    INFO(tr("Bootloader size is %d bytes"), bsize);
    FCLOSE(bd);

    fsize = filesize(fd);
    INFO(tr("Firmware size is %d bytes"), fsize);

    buf = malloc(TOTAL_SIZE);
    if(buf == NULL)
    {
        ERR(tr("Can't allocate %d bytes!"), fsize);
        goto err;
    }
    memset(buf, 0, TOTAL_SIZE);

    INFO(tr("Reading %s into memory..."), firmware);
    if(fread(buf, fsize, 1, fd) != 1)
    {
        ERR(tr("Can't read file %s to memory!"), firmware);
        goto err;
    }
    FCLOSE(fd);

    if(memcmp(buf, "WADF", 4))
    {
        ERR(tr("File %s isn't a valid ChinaChip firmware!"), firmware);
        goto err;
    }

    ccpmp_pos = -1, i = 0x40;
    do
    {
        int filenamesize = le2int(&buf[i]);
        i += sizeof(uint32_t);

        if(!strncmp((char*) &buf[i], "ccpmp.bin", 9))
        {
            ccpmp_pos = i;
            ccpmp_size = le2int(&buf[i + sizeof(uint8_t) + filenamesize]);
        }
        else
            i += filenamesize + le2int(&buf[i + sizeof(uint8_t) + filenamesize])
                 + sizeof(uint32_t) + sizeof(uint8_t);
    } while(ccpmp_pos < 0 && i < fsize);

    if(i >= fsize)
    {
        ERR(tr("Couldn't find ccpmp.bin in %s!"), firmware);
        goto err;
    }
    INFO(tr("Found ccpmp.bin at %d bytes"), ccpmp_pos);

    if(ccpmp_backup)
    {
        bd = fopen(ccpmp_backup, "wb");
        if(!bd)
        {
            ERR(tr("Can't open file %s!"), ccpmp_backup);
            goto err;
        }

        INFO(tr("Writing %d bytes to %s..."), ccpmp_size, ccpmp_backup);
        if(fwrite(&buf[ccpmp_pos], ccpmp_size, 1, bd) != 1)
        {
            ERR(tr("Can't write to file %s!"), ccpmp_backup);
            goto err;
        }
        FCLOSE(bd);
    }

    INFO(tr("Renaming it to ccpmp.old..."));
    buf[ccpmp_pos + 6] = 'o';
    buf[ccpmp_pos + 7] = 'l';
    buf[ccpmp_pos + 8] = 'd';

    bd = fopen(bootloader, "rb");
    if(!bd)
    {
        ERR(tr("Can't open file %s!"), bootloader);
        goto err;
    }

    /* Also include path size */
    ccpmp_pos -= sizeof(uint32_t);

    INFO(tr("Making place for ccpmp.bin..."));
    memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE],
            &buf[ccpmp_pos], fsize - ccpmp_pos);

    INFO(tr("Reading %s into memory..."), bootloader);
    if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE],
             bsize, 1, bd) != 1)
    {
        ERR(tr("Can't read file %s to memory!"), bootloader);
        goto err;
    }
    FCLOSE(bd);

    INFO(tr("Adding header to %s..."), bootloader);
    int2le(&buf[ccpmp_pos            ], 9);                     /* Pathname Size */
    memcpy(&buf[ccpmp_pos + 4        ], "ccpmp.bin", 9);        /* Pathname */
    memset(&buf[ccpmp_pos + 4 + 9    ], 0x20, sizeof(uint8_t)); /* File Type */
    int2le(&buf[ccpmp_pos + 4 + 9 + 1], bsize);                 /* File Size */

    time(&cur_time);
    time_info = localtime(&cur_time);
    if(time_info == NULL)
    {
        ERR(tr("Can't obtain current time!"));
        goto err;
    }

    snprintf(header_time, 13, "%04d%02d%02d%02d%02d", time_info->tm_year + 1900,
                                              time_info->tm_mon,
                                              time_info->tm_mday,
                                              time_info->tm_hour,
                                              time_info->tm_min);

    INFO(tr("Computing checksum..."));
    for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4)
        checksum += le2int(&buf[i]);

    INFO(tr("Updating main header..."));
    memcpy(&buf[offsetof(struct header, timestamp)], header_time, 12);
    int2le(&buf[offsetof(struct header, size)     ], TOTAL_SIZE);
    int2le(&buf[offsetof(struct header, checksum) ], checksum);

    od = fopen(output, "wb");
    if(!od)
    {
        ERR(tr("Can't open file %s!"), output);
        goto err;
    }

    INFO(tr("Writing output to %s..."), output);
    if(fwrite(buf, TOTAL_SIZE, 1, od) != 1)
    {
        ERR(tr("Can't write to file %s!"), output);
        goto err;
    }
    fclose(od);
    free(buf);

    return 0;

err:
    if(buf)
        free(buf);
    if(fd)
        fclose(fd);
    if(bd)
        fclose(bd);
    if(od)
        fclose(od);

    return -1;
}


#ifdef STANDALONE

#define VERSION         "0.1"
#define PRINT(fmt, ...) fprintf(stderr, fmt"\n", ##__VA_ARGS__)

static void info(void* userdata, char* fmt, ...)
{
    (void)userdata;
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
}

static void err(void* userdata, char* fmt, ...)
{
    (void)userdata;
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
}

void usage(char* name)
{
    PRINT("Usage:");
    PRINT(" %s <firmware> <bootloader> <firmware_output> [backup]", name);
    PRINT("\nExample:");
    PRINT(" %s VX747.HXF bootloader.bin output.HXF ccpmp.bak", name);
    PRINT(" This will copy ccpmp.bin in VX747.HXF as ccpmp.old and replace it"
          " with bootloader.bin, the output will get written to output.HXF."
          " The old ccpmp.bin will get written to ccpmp.bak.");
}

int main(int argc, char* argv[])
{
    PRINT("ChinaChipPatcher v" VERSION " - (C) Maurus Cuelenaere 2009");
    PRINT("This is free software; see the source for copying conditions. There is NO");
    PRINT("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");

    if(argc < 4)
    {
        usage(argv[0]);
        return 1;
    }

    return chinachip_patch(argv[1], argv[2], argv[3], argc > 4 ? argv[4] : NULL,
                           &info, &err, NULL);
}
#endif