se505-buildsystem/tools/gen_image.c

120 lines
2.4 KiB
C
Raw Normal View History

2007-06-15 15:11:23 +02:00
/*
* creates LOAD/Flash images for Siemens SE505
*
* (c) 01/2005 Olaf Rempel <razzor at kopf minus tisch dot de>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include "trxhdr.h"
2007-06-15 15:12:02 +02:00
#include "crc.h"
2007-06-15 15:11:23 +02:00
int main(int argc, char *argv[]) {
2007-06-15 15:12:02 +02:00
struct trx_header *loadtrx, trx;
2007-06-15 15:11:23 +02:00
int i, cnt;
FILE *fp;
char *buf;
if (argc <= 1) {
printf("USAGE: gen_image file.trx file.trx ... > outfile\n");
exit(-1);
}
// this is.. ugly, but works :)
if ((buf = malloc(FILE_MAX_LEN)) == NULL) {
perror("malloc");
exit(-1);
}
// init main header
2007-06-15 15:12:02 +02:00
loadtrx = (void *)buf;
memset(loadtrx, 0, sizeof(struct trx_header));
loadtrx->magic = FILE_MAGIC;
// dunno
loadtrx->flag_version = 0x8000;
2007-06-15 15:11:23 +02:00
cnt = sizeof(struct trx_header);
for (i = 1; i < argc; i++) {
// open file
if ((fp = fopen(argv[i], "r")) < 0) {
perror(argv[i]);
free(buf);
exit(-1);
}
// read header
if ((cnt = fread(&trx, 1, sizeof(struct trx_header), fp)) < 0) {
perror(argv[i]);
fclose(fp);
free(buf);
exit(-1);
} else if (cnt < sizeof(struct trx_header)) {
fprintf(stderr, "Truncated file: %s\n", argv[i]);
fclose(fp);
free(buf);
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[i]);
fclose(fp);
free(buf);
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[i]);
fclose(fp);
free(buf);
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[i]);
fclose(fp);
free(buf);
exit(-1);
}
} else {
fprintf(stderr, "Unknown Header Type: %s\n", argv[i]);
fclose(fp);
free(buf);
exit(-1);
}
// copy header
memcpy(buf + cnt, &trx, sizeof(struct trx_header));
cnt += sizeof(struct trx_header);
// copy file to buffer
while (!feof(fp))
cnt += fread(buf + cnt, 1, 65536, fp);
// close file
fclose(fp);
}
2007-06-15 15:12:02 +02:00
// overall length
loadtrx->len = cnt;
2007-06-15 15:11:23 +02:00
// create chksum without first 3 fields
2007-06-15 15:12:02 +02:00
loadtrx->crc32 = crc32(buf +12, cnt -12, CRC32_INIT_VALUE);
2007-06-15 15:11:23 +02:00
// flush to output
write(1, buf, cnt);
free(buf);
return 0;
}