+int
+trx_check(const char *trxfile, const char *mtd)
+{
+ struct mtd_info_user mtdInfo;
+ int trxfd, fd;
+ size_t count;
+ struct trx_header trx;
+ struct stat trxstat;
+
+ trxfd = open(trxfile,O_RDONLY);
+ if(trxfd < 0) {
+ fprintf(stderr, "Could not open trx image: %s\n", trxfile);
+ exit(1);
+ }
+
+ if (fstat(trxfd,&trxstat) < 0) {
+ fprintf(stderr, "Could not get trx image file status: %s\n", trxfile);
+ close(trxfd);
+ exit(1);
+ }
+
+ count = read(trxfd, &trx, sizeof(struct trx_header));
+ if (count < sizeof(struct trx_header)) {
+ fprintf(stderr, "Could not trx header, file too small (%ld bytes)\n", count);
+ close(trxfd);
+ exit(1);
+ }
+
+ if (trx.magic != TRX_MAGIC || trx.len < sizeof(struct trx_header)) {
+ fprintf(stderr, "Bad trx header\n");
+ fprintf(stderr, "If this is a firmware in bin format, like some of the\n"
+ "original firmware files are, use following command to convert to trx:\n"
+ "dd if=firmware.bin of=firmware.trx bs=32 skip=1\n");
+ close(trxfd);
+ exit(1);
+ }
+
+ lseek(trxfd, 0, SEEK_SET);
+
+ /* check if image fits to mtd device */
+
+ fd = mtd_open(mtd, O_RDWR);
+ if(fd < 0) {
+ fprintf(stderr, "Could not open mtd device: %s\n", mtd);
+ exit(1);
+ }
+
+ if(ioctl(fd, MEMGETINFO, &mtdInfo)) {
+ fprintf(stderr, "Could not get MTD device info from %s\n", mtd);
+ close(fd);
+ exit(1);
+ }
+
+ if(mtdInfo.size < trxstat.st_size) {
+ fprintf(stderr, "Image too big for partition: %s\n", mtd);
+ close(trxfd);
+ close(fd);
+ exit(1);
+ }
+
+ printf("Writing %s to %s ...\n", trxfile, mtd);
+
+ close(fd);
+
+ return(trxfd);
+}
+