Rafaël Carré | c0bd417 | 2010-06-17 16:59:51 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2008 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 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <dirent.h> |
| 31 | |
| 32 | #define VERSION "0.1" |
| 33 | |
| 34 | static unsigned char* int2le(unsigned int val) |
| 35 | { |
| 36 | static unsigned char addr[4]; |
| 37 | addr[0] = val & 0xff; |
| 38 | addr[1] = (val >> 8) & 0xff; |
| 39 | addr[2] = (val >> 16) & 0xff; |
| 40 | addr[3] = (val >> 24) & 0xff; |
| 41 | return addr; |
| 42 | } |
| 43 | |
| 44 | static unsigned int le2int(unsigned char* buf) |
| 45 | { |
| 46 | unsigned int res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 47 | |
| 48 | return res; |
| 49 | } |
| 50 | |
| 51 | unsigned int _filesize(FILE* fd) |
| 52 | { |
| 53 | unsigned int tmp, oldpos; |
| 54 | oldpos = ftell(fd); |
| 55 | fseek(fd, 0, SEEK_END); |
| 56 | tmp = ftell(fd); |
| 57 | fseek(fd, oldpos, SEEK_SET); |
| 58 | return tmp; |
| 59 | } |
| 60 | |
| 61 | static void print_usage(void) |
| 62 | { |
| 63 | #ifdef _WIN32 |
| 64 | fprintf(stderr, "Usage: hxfreplace.exe [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); |
| 65 | fprintf(stderr, "Example: hxfreplace.exe VX747.HXF out.hxf ccpmp.bin\n\n"); |
| 66 | #else |
| 67 | fprintf(stderr, "Usage: HXFreplace [IN_FW] [OUT_FW] [BIN_FILE]\n\n"); |
| 68 | fprintf(stderr, "Example: HXFreplace VX747.HXF out.hxf ccpmp.bin\n\n"); |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | static int checksum(FILE *file) |
| 73 | { |
| 74 | int oldpos = ftell(file); |
| 75 | int ret=0, i, filesize = _filesize(file)-0x40; |
| 76 | unsigned char *buf; |
| 77 | |
| 78 | buf = (unsigned char*)malloc(filesize); |
| 79 | |
| 80 | if(buf == NULL) |
| 81 | { |
| 82 | fseek(file, oldpos, SEEK_SET); |
| 83 | fprintf(stderr, "[ERR] Error while allocating memory\n"); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | fseek(file, 0x40, SEEK_SET); |
| 88 | if(fread(buf, filesize, 1, file) != 1) |
| 89 | { |
| 90 | free(buf); |
| 91 | fseek(file, oldpos, SEEK_SET); |
| 92 | fprintf(stderr, "[ERR] Error while reading from file\n"); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | fprintf(stderr, "[INFO] Computing checksum..."); |
| 97 | |
| 98 | for(i = 0; i < filesize; i+=4) |
| 99 | ret += le2int(&buf[i]); |
| 100 | |
| 101 | free(buf); |
| 102 | fseek(file, oldpos, SEEK_SET); |
| 103 | |
| 104 | fprintf(stderr, " Done!\n"); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | int main(int argc, char *argv[]) |
| 109 | { |
| 110 | FILE *infile, *outfile, *fw; |
| 111 | |
| 112 | fprintf(stderr, "HXFreplace v" VERSION " - (C) 2008 Maurus Cuelenaere\n"); |
| 113 | fprintf(stderr, "This is free software; see the source for copying conditions. There is NO\n"); |
| 114 | fprintf(stderr, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"); |
| 115 | |
| 116 | if(argc != 4) |
| 117 | { |
| 118 | print_usage(); |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | if((infile = fopen(argv[1], "rb")) == NULL) |
| 123 | { |
| 124 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[1]); |
| 125 | return 2; |
| 126 | } |
| 127 | |
| 128 | if(fseek(infile, 0x40, SEEK_SET) != 0) |
| 129 | { |
| 130 | fprintf(stderr, "[ERR] Cannot seek to 0x40\n"); |
| 131 | fclose(infile); |
| 132 | return 3; |
| 133 | } |
| 134 | |
| 135 | fprintf(stderr, "[INFO] Searching for ccpmp.bin...\n"); |
| 136 | |
| 137 | int found = -1; |
| 138 | int filenamesize; |
| 139 | char *filename; |
| 140 | unsigned char tmp[4]; |
| 141 | |
| 142 | #define READ(x, len) if(fread(x, len, 1, infile) != 1) \ |
| 143 | { \ |
| 144 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[1]); \ |
| 145 | fclose(infile); \ |
| 146 | return 4; \ |
| 147 | } |
| 148 | while(found < 0) |
| 149 | { |
| 150 | READ(&tmp[0], 4); |
| 151 | filenamesize = le2int(tmp); |
| 152 | filename = (char*)malloc(filenamesize); |
| 153 | READ(filename, filenamesize); |
| 154 | if(strcmp(filename, "ccpmp.bin") == 0) |
| 155 | found = ftell(infile); |
| 156 | else |
| 157 | { |
| 158 | READ(&tmp[0], 4); |
| 159 | fseek(infile, le2int(tmp), SEEK_CUR); |
| 160 | } |
| 161 | free(filename); |
| 162 | } |
| 163 | |
| 164 | fprintf(stderr, "[INFO] Found ccpmp.bin at 0x%x\n", found); |
| 165 | |
| 166 | if((outfile = fopen(argv[2], "wb+")) == NULL) |
| 167 | { |
| 168 | fclose(infile); |
| 169 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[2]); |
| 170 | return 5; |
| 171 | } |
| 172 | |
| 173 | #define WRITE(x, len) if(fwrite(x, len, 1, outfile) != 1) \ |
| 174 | { \ |
| 175 | fprintf(stderr, "[ERR] Cannot write to %s\n", argv[2]); \ |
| 176 | fclose(outfile); \ |
| 177 | if(fw != NULL) \ |
| 178 | fclose(fw); \ |
| 179 | return 5; \ |
| 180 | } |
| 181 | |
| 182 | unsigned char* buffer; |
| 183 | |
| 184 | buffer = (unsigned char*)malloc(found); |
| 185 | fseek(infile, 0, SEEK_SET); |
| 186 | READ(buffer, found); |
| 187 | WRITE(buffer, found); |
| 188 | free(buffer); |
| 189 | |
| 190 | if((fw = fopen(argv[3], "rb")) == NULL) |
| 191 | { |
| 192 | fclose(infile); |
| 193 | fclose(outfile); |
| 194 | fprintf(stderr, "[ERR] Cannot open %s\n", argv[3]); |
| 195 | } |
| 196 | |
| 197 | int fw_filesize = _filesize(fw); |
| 198 | |
| 199 | #define READ2(x, len) if(fread(x, len, 1, fw) != 1) \ |
| 200 | { \ |
| 201 | fprintf(stderr, "[ERR] Cannot read from %s\n", argv[3]); \ |
| 202 | fclose(infile); \ |
| 203 | fclose(outfile); \ |
| 204 | return 6; \ |
| 205 | } |
| 206 | buffer = (unsigned char*)malloc(fw_filesize); |
| 207 | READ2(buffer, fw_filesize); |
| 208 | fputc(0x20, outfile); /* Padding */ |
| 209 | WRITE(int2le(fw_filesize), 4); |
| 210 | WRITE(buffer, fw_filesize); |
| 211 | free(buffer); |
| 212 | fclose(fw); |
| 213 | fw = NULL; |
| 214 | |
| 215 | fseek(infile, found+1, SEEK_SET); |
| 216 | READ(&tmp, 4); |
| 217 | if(fseek(infile, le2int(&tmp[0]), SEEK_CUR) != 0) |
| 218 | { |
| 219 | fprintf(stderr, "[INFO] Cannot seek into %s\n", argv[1]); |
| 220 | fclose(infile); |
| 221 | fclose(outfile); |
| 222 | return 7; |
| 223 | } |
| 224 | found = ftell(infile); |
| 225 | |
| 226 | int other_size = _filesize(infile) - found; |
| 227 | buffer = (unsigned char*)malloc(other_size); |
| 228 | READ(buffer, other_size); |
| 229 | WRITE(buffer, other_size); |
| 230 | free(buffer); |
| 231 | fclose(infile); |
| 232 | |
| 233 | fflush(outfile); |
| 234 | fseek(outfile, 0x14, SEEK_SET); |
| 235 | WRITE(int2le(_filesize(outfile)), 4); |
| 236 | WRITE(int2le(checksum(outfile)), 4); |
| 237 | fclose(outfile); |
| 238 | |
| 239 | fprintf(stderr, "[INFO] Done!\n"); |
| 240 | |
| 241 | return 0; |
| 242 | } |