- mtdEraseInfo.start = 0;
- mtdEraseInfo.length = trxstat.st_size & ~(mtdInfo.erasesize -1);
- if(trxstat.st_size % mtdInfo.erasesize) mtdEraseInfo.length += mtdInfo.erasesize;
-
- /* erase the chunk */
- if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
- fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
- exit(1);
- }
-
- size = trxstat.st_size;
- i = BUFSIZE;
- written = 0;
-
- while (size) {
- if (size < BUFSIZE) i = size;
- read(trxfd,src,i);
- result = write(fd,src,i);
- if (i != result) {
+ r = w = e = 0;
+ fprintf(stderr, " [ ]");
+
+ for (;;) {
+ /* buffer may contain data already (from trx check) */
+ r = buflen;
+ r += read(imagefd, buf + buflen, BUFSIZE - buflen);
+ w += r;
+
+ /* EOF */
+ if (r <= 0) break;
+
+ /* need to erase the next block before writing data to it */
+ while (w > e) {
+ mtdEraseInfo.start = e;
+ mtdEraseInfo.length = mtdInfo.erasesize;
+
+ fprintf(stderr, "\b\b\b[e]");
+ /* erase the chunk */
+ if (ioctl (fd,MEMERASE,&mtdEraseInfo) < 0) {
+ fprintf(stderr, "Erasing mtd failed: %s\n", mtd);
+ exit(1);
+ }
+ e += mtdInfo.erasesize;
+ }
+
+ fprintf(stderr, "\b\b\b[w]");
+
+ if ((result = write(fd, buf, r)) < r) {