/* * split LOAD/Flash images from Siemens SE505 * * (c) 01/2005 Olaf Rempel */ #include #include #include #include #include #include "trxhdr.h" #define BUFSIZE 32768 int main(int argc, char *argv[]) { struct trx_header trx; int i = 1, len, cnt; FILE *fp, *out; char *buf, filename[128]; if (argc <= 1) { printf("USAGE: split_image firmware.img\n"); exit(-1); } // open file if ((fp = fopen(argv[1], "r")) < 0) { perror(argv[1]); exit(-1); } // read main header if ((cnt = fread(&trx, 1, sizeof(struct trx_header), fp)) < 0) { perror(argv[1]); fclose(fp); exit(-1); } else if (cnt < sizeof(struct trx_header)) { fprintf(stderr, "Truncated file: %s\n", argv[1]); fclose(fp); exit(-1); } else if (trx.magic != FILE_MAGIC || trx.len > FILE_MAX_LEN || trx.len < sizeof(struct trx_header)) { fprintf(stderr, "Bad LOAD Header: %s\n", argv[1]); fclose(fp); exit(-1); } len = trx.len - sizeof(struct trx_header); if ((buf = malloc(BUFSIZE)) == NULL) { perror("malloc"); exit(-1); } while (len > 0) { // read header if ((cnt = fread(&trx, 1, sizeof(struct trx_header), fp)) < 0) { perror(argv[1]); free(buf); fclose(fp); exit(-1); } else if (cnt < sizeof(struct trx_header)) { fprintf(stderr, "Truncated file: %s\n", argv[1]); free(buf); fclose(fp); exit(-1); } else if (trx.magic == TRX_MAGIC) { if (trx.len > TRX_MAX_LEN || trx.len < sizeof(struct trx_header)) { fprintf(stderr, "Bad TRX Header: %s\n", argv[1]); free(buf); fclose(fp); exit(-1); } } else if (trx.magic == PMON_MAGIC) { if (trx.len > PMON_MAX_LEN || trx.len < sizeof(struct trx_header)) { fprintf(stderr, "Bad PMON Header: %s\n", argv[1]); free(buf); fclose(fp); exit(-1); } } else if (trx.magic == NVAR_MAGIC) { if (trx.len > NVRAM_MAX_LEN || trx.len < sizeof(struct trx_header)) { fprintf(stderr, "Bad NVRAM Header: %s\n", argv[1]); free(buf); fclose(fp); exit(-1); } } else { fprintf(stderr, "Unknown Header Type: %s\n", argv[1]); free(buf); fclose(fp); exit(-1); } // bad bad (unknown length) snprintf(filename, sizeof(filename), "%s.%d.trx", argv[1], i++); // open file if ((out = fopen(filename, "w")) < 0) { perror(filename); free(buf); exit(-1); } printf("%s : %d bytes\n", filename, trx.len); fwrite(&trx, 1, sizeof(struct trx_header), out); len -= trx.len; trx.len -= sizeof(struct trx_header); while (trx.len > 0 && cnt > 0) { cnt = fread(buf, 1, (trx.len <= BUFSIZE ? trx.len : BUFSIZE), fp); fwrite(buf, 1, cnt, out); trx.len -= cnt; } fclose(out); } free(buf); fclose(fp); return 0; }