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