--- embedtools/src/imgupd.c 2014/02/05 22:53:12 1.4 +++ embedtools/src/imgupd.c 2014/02/06 01:23:56 1.6.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: imgupd.c,v 1.4 2014/02/05 22:53:12 misho Exp $ + * $Id: imgupd.c,v 1.6.2.4 2014/02/06 01:23:56 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following @@ -47,7 +47,7 @@ SUCH DAMAGE. char imgName[PATH_MAX], imgFile[PATH_MAX]; -off_t imgSize, iSize; +off_t imgSize, iSize, fromBegin; int Verbose, bufSize = IMGBUF_SIZE; extern char compiled[], compiledby[], compilehost[]; @@ -61,6 +61,7 @@ Usage() "\t-v\t\tVerbose ...\n" "\t-R\t\tReboot system after complete\n" "\t-p\t\tPipe suitable transfer on little chunks\n" + "\t-i\t\tStart fill storage from begin\n" "\t-g\t\tGet image from Storage\n" "\t-t\t\tTruncate Storage file name\n" "\t-s \tStorage size (required for stdin)\n" @@ -77,19 +78,25 @@ EmptyStore(int img) VERB(1) printf("Erase store %s\n", imgName); - iSize = lseek(img, 0, SEEK_END); - if (iSize == -1) { - ESYSERR(0); - return -1; + if (!fromBegin) { + iSize = lseek(img, 0, SEEK_END); + if (iSize == -1) { + ESYSERR(0); + return -1; + } else + imgSize += E_ALIGN(iSize, bufSize); } else - imgSize += E_ALIGN(iSize, bufSize); + iSize ^= iSize; memset(buf, 0, sizeof buf); for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++) - if ((wlen = write(img, buf, sizeof buf)) == -1 || - wlen != sizeof buf) { - EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", - i, wlen, sizeof buf); + if ((wlen = write(img, buf, bufSize)) == -1 || + (wlen && wlen != bufSize)) { + if (wlen == -1) + ESYSERR(0); + else + EERROR(EIO, "Error at chunk %d init %d bytes, " + "should be %u\n", i, wlen, bufSize); return -1; } else VERB(1) printf("+Written chunk #%d\n", i); @@ -110,7 +117,7 @@ FillStore(int img, int fd) for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++, j++) { memset(buf, 0, sizeof buf); - rlen = read(fd, buf, sizeof buf); + rlen = read(fd, buf, bufSize); if (rlen == -1) { ESYSERR(0); return -1; @@ -119,13 +126,13 @@ FillStore(int img, int fd) else VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j); - wlen = write(img, buf, rlen); + wlen = write(img, buf, bufSize); if (wlen == -1) { ESYSERR(0); return -1; - } else if (!wlen || wlen != rlen) { - EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", - rlen, wlen); + } else if (wlen && wlen != bufSize) { + EERROR(EIO, "Error at chunk %d written %d bytes, " + "should be %u\n", i, wlen, bufSize); } else VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i); } @@ -139,7 +146,7 @@ main(int argc, char **argv) char ch, m = 0, R = 0; int fd, img, tr = 0; - while ((ch = getopt(argc, argv, "hvRpgts:f:")) != -1) + while ((ch = getopt(argc, argv, "hvRipgts:f:")) != -1) switch (ch) { case 'f': strlcpy(imgName, optarg, sizeof imgName); @@ -154,12 +161,14 @@ main(int argc, char **argv) case 't': tr = O_TRUNC; break; - case 'g': - m = 1; - break; case 'p': bufSize = IMGBUF_SIZE2; break; + case 'g': + m = 1; + case 'i': + fromBegin = 1; + break; case 'R': R = 1; break; @@ -265,6 +274,7 @@ main(int argc, char **argv) return 4; } } else { /* GET */ + iSize ^= iSize; if (EmptyStore(fd) == -1) { if (fd > 2) close(fd);