107 lines
2.0 KiB
C
107 lines
2.0 KiB
C
|
/*
|
||
|
* split LOAD/Flash images from Siemens SE505
|
||
|
*
|
||
|
* (c) 01/2005 Olaf Rempel <razzor at kopf minus tisch dot de>
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#define __USE_GNU
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <sys/types.h>
|
||
|
#include "trxhdr.h"
|
||
|
|
||
|
#define BUFSIZE 32768
|
||
|
#define CRAMFS_MAGIC "\x45\x3D\xCD\x28"
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
struct trx_header trx;
|
||
|
int len, cnt;
|
||
|
FILE *fp, *out;
|
||
|
char *buf, *p, filename[128];
|
||
|
u_int32_t *fs;
|
||
|
|
||
|
if (argc <= 1) {
|
||
|
printf("USAGE: split_linux linux.trx\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 != TRX_MAGIC || trx.len > TRX_MAX_LEN || trx.len < sizeof(struct trx_header)) {
|
||
|
fprintf(stderr, "Bad TRX Header: %s\n", argv[1]);
|
||
|
fclose(fp);
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
len = trx.len - sizeof(struct trx_header);
|
||
|
|
||
|
// big & ugly
|
||
|
if ((buf = malloc(TRX_MAX_LEN)) == NULL) {
|
||
|
perror("malloc");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
// slurp in
|
||
|
len = fread(buf, 1, TRX_MAX_LEN, fp);
|
||
|
|
||
|
// search header
|
||
|
if ((p = memmem(buf, len, CRAMFS_MAGIC, 4)) == NULL) {
|
||
|
fprintf(stderr, "No CRAMFS Header found\n");
|
||
|
free(buf);
|
||
|
fclose(fp);
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
cnt = (int)(p - buf);
|
||
|
|
||
|
// open file
|
||
|
snprintf(filename, sizeof(filename), "%s.kernel", argv[1]);
|
||
|
if ((out = fopen(filename, "w")) < 0) {
|
||
|
perror(filename);
|
||
|
free(buf);
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
printf("%s: %d bytes\n", filename, cnt);
|
||
|
fwrite(buf, 1, cnt, out);
|
||
|
fclose(out);
|
||
|
|
||
|
// open file
|
||
|
snprintf(filename, sizeof(filename), "%s.cramfs", argv[1]);
|
||
|
if ((out = fopen(filename, "w")) < 0) {
|
||
|
perror(filename);
|
||
|
free(buf);
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
// "echte" fs laenge auslesen
|
||
|
fs = (u_int32_t *)(p +4);
|
||
|
printf("%s: %d bytes\n", filename, *fs);
|
||
|
fwrite(p, 1, *fs, out);
|
||
|
fclose(out);
|
||
|
|
||
|
free(buf);
|
||
|
fclose(fp);
|
||
|
return 0;
|
||
|
}
|
||
|
|