Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 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 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 30 | #define tr(x) x /* Qt translation support */ |
| 31 | |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 32 | /* From http://www.rockbox.org/wiki/ChinaChip */ |
| 33 | struct 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 | |
| 44 | static 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 | |
| 52 | static inline unsigned int le2int(unsigned char* buf) |
| 53 | { |
| 54 | return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 55 | } |
| 56 | |
| 57 | static 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) |
| 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) |
| 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 Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 94 | ERR(tr("Can't open file %s!"), firmware); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 95 | goto err; |
| 96 | } |
| 97 | bd = fopen(bootloader, "rb"); |
| 98 | if(!bd) |
| 99 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 100 | ERR(tr("Can't open file %s!"), bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 101 | goto err; |
| 102 | } |
| 103 | |
| 104 | bsize = filesize(bd); |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 105 | INFO(tr("Bootloader size is %d bytes"), bsize); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 106 | FCLOSE(bd); |
| 107 | |
| 108 | fsize = filesize(fd); |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 109 | INFO(tr("Firmware size is %d bytes"), fsize); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 110 | |
| 111 | buf = malloc(TOTAL_SIZE); |
| 112 | if(buf == NULL) |
| 113 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 114 | ERR(tr("Can't allocate %d bytes!"), fsize); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 115 | goto err; |
| 116 | } |
| 117 | memset(buf, 0, TOTAL_SIZE); |
| 118 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 119 | INFO(tr("Reading %s into memory..."), firmware); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 120 | if(fread(buf, fsize, 1, fd) != 1) |
| 121 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 122 | ERR(tr("Can't read file %s to memory!"), firmware); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 123 | goto err; |
| 124 | } |
| 125 | FCLOSE(fd); |
| 126 | |
| 127 | if(memcmp(buf, "WADF", 4)) |
| 128 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 129 | ERR(tr("File %s isn't a valid ChinaChip firmware!"), firmware); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 130 | 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 Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 151 | ERR(tr("Couldn't find ccpmp.bin in %s!"), firmware); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 152 | goto err; |
| 153 | } |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 154 | INFO(tr("Found ccpmp.bin at %d bytes"), ccpmp_pos); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 155 | |
| 156 | if(ccpmp_backup) |
| 157 | { |
Maurus Cuelenaere | 98f5c30 | 2009-09-14 12:29:34 +0000 | [diff] [blame] | 158 | int ccpmp_data_pos = ccpmp_pos + 9; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 159 | bd = fopen(ccpmp_backup, "wb"); |
| 160 | if(!bd) |
| 161 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 162 | ERR(tr("Can't open file %s!"), ccpmp_backup); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 163 | goto err; |
| 164 | } |
| 165 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 166 | INFO(tr("Writing %d bytes to %s..."), ccpmp_size, ccpmp_backup); |
Maurus Cuelenaere | 98f5c30 | 2009-09-14 12:29:34 +0000 | [diff] [blame] | 167 | if(fwrite(&buf[ccpmp_data_pos], ccpmp_size, 1, bd) != 1) |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 168 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 169 | ERR(tr("Can't write to file %s!"), ccpmp_backup); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 170 | goto err; |
| 171 | } |
| 172 | FCLOSE(bd); |
| 173 | } |
| 174 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 175 | INFO(tr("Renaming it to ccpmp.old...")); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 176 | 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 Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 183 | ERR(tr("Can't open file %s!"), bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 184 | goto err; |
| 185 | } |
| 186 | |
| 187 | /* Also include path size */ |
| 188 | ccpmp_pos -= sizeof(uint32_t); |
| 189 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 190 | INFO(tr("Making place for ccpmp.bin...")); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 191 | memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE], |
| 192 | &buf[ccpmp_pos], fsize - ccpmp_pos); |
| 193 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 194 | INFO(tr("Reading %s into memory..."), bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 195 | if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE], |
| 196 | bsize, 1, bd) != 1) |
| 197 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 198 | ERR(tr("Can't read file %s to memory!"), bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 199 | goto err; |
| 200 | } |
| 201 | FCLOSE(bd); |
| 202 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 203 | INFO(tr("Adding header to %s..."), bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 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 */ |
| 208 | |
| 209 | time(&cur_time); |
| 210 | time_info = localtime(&cur_time); |
| 211 | if(time_info == NULL) |
| 212 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 213 | ERR(tr("Can't obtain current time!")); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 214 | 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 Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 223 | INFO(tr("Computing checksum...")); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 224 | for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4) |
| 225 | checksum += le2int(&buf[i]); |
| 226 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 227 | INFO(tr("Updating main header...")); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 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); |
| 231 | |
| 232 | od = fopen(output, "wb"); |
| 233 | if(!od) |
| 234 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 235 | ERR(tr("Can't open file %s!"), output); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 236 | goto err; |
| 237 | } |
| 238 | |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 239 | INFO(tr("Writing output to %s..."), output); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 240 | if(fwrite(buf, TOTAL_SIZE, 1, od) != 1) |
| 241 | { |
Maurus Cuelenaere | 11826e9 | 2009-08-21 15:46:15 +0000 | [diff] [blame] | 242 | ERR(tr("Can't write to file %s!"), output); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 243 | goto err; |
| 244 | } |
| 245 | fclose(od); |
| 246 | free(buf); |
| 247 | |
| 248 | return 0; |
| 249 | |
| 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); |
| 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 | |
| 269 | static 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 | |
| 278 | static 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 | |
| 287 | void 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 | |
| 298 | int 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 |