se505-buildsystem/tools/trx.c

89 lines
1.4 KiB
C

/*
* 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"
#include "crc.h"
int main(int argc, char *argv[]) {
struct trx_header *trx;
int i, cnt, fcnt = 0;
FILE *fp;
char *buf;
if (argc <= 1) {
printf("USAGE: trx file1 [file2] [file3] > outfile.trx\n");
exit(-1);
}
// this is.. ugly, but works :)
if ((buf = malloc(TRX_MAX_LEN)) == NULL) {
perror("malloc");
exit(-1);
}
// init main header
trx = (void *)buf;
memset(trx, 0, sizeof(struct trx_header));
trx->magic = TRX_MAGIC;
trx->flag_version = TRX_VERSION << 16;
// copy header
cnt = sizeof(struct trx_header);
for (i = 1; i < argc; i++) {
if (fcnt == 2) {
fprintf(stdout, "max. 3 Files\n");
free(buf);
exit(-1);
}
// align offsets
while (cnt % 4)
buf[cnt++] = 0;
trx->offsets[fcnt++] = cnt;
// open file
if ((fp = fopen(argv[i], "r")) < 0) {
perror(argv[i]);
free(buf);
exit(-1);
}
// copy file to buffer
while (!feof(fp))
cnt += fread(buf + cnt, 1, 65536, fp);
// close file
fclose(fp);
}
// align size (to page?)
while (cnt % 0x1000)
buf[cnt++] = 0;
trx->len = cnt;
// calc crc32
trx->crc32 = crc32(buf +12, cnt -12, CRC32_INIT_VALUE);
// flush to output
write(1, buf, cnt);
free(buf);
return 0;
}