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 | |
| 30 | /* From http://www.rockbox.org/wiki/ChinaChip */ |
| 31 | struct 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 | |
| 42 | static 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 | |
| 50 | static inline unsigned int le2int(unsigned char* buf) |
| 51 | { |
| 52 | return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 53 | } |
| 54 | |
| 55 | static 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 | |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 64 | #define FCLOSE(fd) fclose(fd); fd = NULL; |
| 65 | #define CCPMPBIN_HEADER_SIZE (sizeof(uint32_t)*2 + sizeof(uint8_t) + 9) |
| 66 | #define TOTAL_SIZE (fsize + CCPMPBIN_HEADER_SIZE + bsize) |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 67 | enum cc_error chinachip_patch(const char* firmware, const char* bootloader, |
| 68 | const char* output, const char* ccpmp_backup) |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 69 | { |
| 70 | char header_time[13]; |
| 71 | time_t cur_time; |
| 72 | struct tm* time_info; |
| 73 | unsigned char* buf = NULL; |
| 74 | FILE *fd = NULL, *bd = NULL, *od = NULL; |
| 75 | unsigned int ccpmp_size = 0, i, fsize, bsize; |
| 76 | signed int checksum = 0, ccpmp_pos; |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 77 | int result = E_OK; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 78 | |
| 79 | fd = fopen(firmware, "rb"); |
| 80 | if(!fd) |
| 81 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 82 | fprintf(stderr, "[ERR] Can't open file %s!\n", firmware); |
| 83 | result = E_OPEN_FIRMWARE; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 84 | goto err; |
| 85 | } |
| 86 | bd = fopen(bootloader, "rb"); |
| 87 | if(!bd) |
| 88 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 89 | fprintf(stderr, "[ERR] Can't open file %s!\n", bootloader); |
| 90 | result = E_OPEN_BOOTLOADER; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 91 | goto err; |
| 92 | } |
| 93 | |
| 94 | bsize = filesize(bd); |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 95 | fprintf(stderr, "[INFO] Bootloader size is %d bytes\n", bsize); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 96 | FCLOSE(bd); |
| 97 | |
| 98 | fsize = filesize(fd); |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 99 | fprintf(stderr, "[INFO] Firmware size is %d bytes\n", fsize); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 100 | |
| 101 | buf = malloc(TOTAL_SIZE); |
| 102 | if(buf == NULL) |
| 103 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 104 | fprintf(stderr, "[ERR] Can't allocate %d bytes!\n", fsize); |
| 105 | result = E_MEMALLOC; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 106 | goto err; |
| 107 | } |
| 108 | memset(buf, 0, TOTAL_SIZE); |
| 109 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 110 | fprintf(stderr, "[INFO] Reading %s into memory...\n", firmware); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 111 | if(fread(buf, fsize, 1, fd) != 1) |
| 112 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 113 | fprintf(stderr, "[ERR] Can't read file %s to memory!\n", firmware); |
| 114 | result = E_LOAD_FIRMWARE; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 115 | goto err; |
| 116 | } |
| 117 | FCLOSE(fd); |
| 118 | |
| 119 | if(memcmp(buf, "WADF", 4)) |
| 120 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 121 | fprintf(stderr, "[ERR] File %s isn't a valid ChinaChip firmware!\n", firmware); |
| 122 | result = E_INVALID_FILE; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 123 | goto err; |
| 124 | } |
| 125 | |
| 126 | ccpmp_pos = -1, i = 0x40; |
| 127 | do |
| 128 | { |
| 129 | int filenamesize = le2int(&buf[i]); |
| 130 | i += sizeof(uint32_t); |
| 131 | |
| 132 | if(!strncmp((char*) &buf[i], "ccpmp.bin", 9)) |
| 133 | { |
| 134 | ccpmp_pos = i; |
| 135 | ccpmp_size = le2int(&buf[i + sizeof(uint8_t) + filenamesize]); |
| 136 | } |
| 137 | else |
| 138 | i += filenamesize + le2int(&buf[i + sizeof(uint8_t) + filenamesize]) |
| 139 | + sizeof(uint32_t) + sizeof(uint8_t); |
| 140 | } while(ccpmp_pos < 0 && i < fsize); |
| 141 | |
| 142 | if(i >= fsize) |
| 143 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 144 | fprintf(stderr, "[ERR] Couldn't find ccpmp.bin in %s!\n", firmware); |
| 145 | result = E_NO_CCPMP; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 146 | goto err; |
| 147 | } |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 148 | fprintf(stderr, "[INFO] Found ccpmp.bin at %d bytes\n", ccpmp_pos); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 149 | |
| 150 | if(ccpmp_backup) |
| 151 | { |
Maurus Cuelenaere | 98f5c30 | 2009-09-14 12:29:34 +0000 | [diff] [blame] | 152 | int ccpmp_data_pos = ccpmp_pos + 9; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 153 | bd = fopen(ccpmp_backup, "wb"); |
| 154 | if(!bd) |
| 155 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 156 | fprintf(stderr, "[ERR] Can't open file %s!\n", ccpmp_backup); |
| 157 | result = E_OPEN_BACKUP; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 158 | goto err; |
| 159 | } |
| 160 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 161 | fprintf(stderr, "[INFO] Writing %d bytes to %s...\n", ccpmp_size, ccpmp_backup); |
Maurus Cuelenaere | 98f5c30 | 2009-09-14 12:29:34 +0000 | [diff] [blame] | 162 | if(fwrite(&buf[ccpmp_data_pos], ccpmp_size, 1, bd) != 1) |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 163 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 164 | fprintf(stderr, "[ERR] Can't write to file %s!\n", ccpmp_backup); |
| 165 | result = E_WRITE_BACKUP; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 166 | goto err; |
| 167 | } |
| 168 | FCLOSE(bd); |
| 169 | } |
| 170 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 171 | fprintf(stderr, "[INFO] Renaming it to ccpmp.old...\n"); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 172 | buf[ccpmp_pos + 6] = 'o'; |
| 173 | buf[ccpmp_pos + 7] = 'l'; |
| 174 | buf[ccpmp_pos + 8] = 'd'; |
| 175 | |
| 176 | bd = fopen(bootloader, "rb"); |
| 177 | if(!bd) |
| 178 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 179 | fprintf(stderr, "[ERR] Can't open file %s!\n", bootloader); |
| 180 | result = E_OPEN_BOOTLOADER; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 181 | goto err; |
| 182 | } |
| 183 | |
| 184 | /* Also include path size */ |
| 185 | ccpmp_pos -= sizeof(uint32_t); |
| 186 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 187 | fprintf(stderr, "[INFO] Making place for ccpmp.bin...\n"); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 188 | memmove(&buf[ccpmp_pos + bsize + CCPMPBIN_HEADER_SIZE], |
| 189 | &buf[ccpmp_pos], fsize - ccpmp_pos); |
| 190 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 191 | fprintf(stderr, "[INFO] Reading %s into memory...\n", bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 192 | if(fread(&buf[ccpmp_pos + CCPMPBIN_HEADER_SIZE], |
| 193 | bsize, 1, bd) != 1) |
| 194 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 195 | fprintf(stderr, "[ERR] Can't read file %s to memory!\n", bootloader); |
| 196 | result = E_LOAD_BOOTLOADER; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 197 | goto err; |
| 198 | } |
| 199 | FCLOSE(bd); |
| 200 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 201 | fprintf(stderr, "[INFO] Adding header to %s...\n", bootloader); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 202 | int2le(&buf[ccpmp_pos ], 9); /* Pathname Size */ |
| 203 | memcpy(&buf[ccpmp_pos + 4 ], "ccpmp.bin", 9); /* Pathname */ |
| 204 | memset(&buf[ccpmp_pos + 4 + 9 ], 0x20, sizeof(uint8_t)); /* File Type */ |
| 205 | int2le(&buf[ccpmp_pos + 4 + 9 + 1], bsize); /* File Size */ |
| 206 | |
| 207 | time(&cur_time); |
| 208 | time_info = localtime(&cur_time); |
| 209 | if(time_info == NULL) |
| 210 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 211 | fprintf(stderr, "[ERR] Can't obtain current time!\n"); |
| 212 | result = E_GET_TIME; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 213 | goto err; |
| 214 | } |
| 215 | |
| 216 | snprintf(header_time, 13, "%04d%02d%02d%02d%02d", time_info->tm_year + 1900, |
| 217 | time_info->tm_mon, |
| 218 | time_info->tm_mday, |
| 219 | time_info->tm_hour, |
| 220 | time_info->tm_min); |
| 221 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 222 | fprintf(stderr, "[INFO] Computing checksum...\n"); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 223 | for(i = sizeof(struct header); i < TOTAL_SIZE; i+=4) |
| 224 | checksum += le2int(&buf[i]); |
| 225 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 226 | fprintf(stderr, "[INFO] Updating main header...\n"); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 227 | memcpy(&buf[offsetof(struct header, timestamp)], header_time, 12); |
| 228 | int2le(&buf[offsetof(struct header, size) ], TOTAL_SIZE); |
| 229 | int2le(&buf[offsetof(struct header, checksum) ], checksum); |
| 230 | |
| 231 | od = fopen(output, "wb"); |
| 232 | if(!od) |
| 233 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 234 | fprintf(stderr, "[ERR] Can't open file %s!\n", output); |
| 235 | result = E_OPEN_OUTFILE; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 236 | goto err; |
| 237 | } |
| 238 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 239 | fprintf(stderr, "[INFO] Writing output to %s...\n", output); |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 240 | if(fwrite(buf, TOTAL_SIZE, 1, od) != 1) |
| 241 | { |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 242 | fprintf(stderr, "[ERR] Can't write to file %s!\n", output); |
| 243 | result = E_WRITE_OUTFILE; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 244 | goto err; |
| 245 | } |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 246 | |
| 247 | err: |
| 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 | |
Dominik Riebeling | 059cb71 | 2011-12-03 09:41:44 +0000 | [diff] [blame] | 257 | return result; |
Maurus Cuelenaere | e8c71aa | 2009-08-16 20:39:00 +0000 | [diff] [blame] | 258 | } |
| 259 | |