--- embedtools/src/imgupd.c 2014/02/05 22:53:12 1.4 +++ embedtools/src/imgupd.c 2017/01/22 20:40:10 1.7.4.1 @@ -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.7.4.1 2017/01/22 20:40:10 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2014 +Copyright 2004 - 2017 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,11 +44,12 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" +#include "imgupd.h" char imgName[PATH_MAX], imgFile[PATH_MAX]; -off_t imgSize, iSize; -int Verbose, bufSize = IMGBUF_SIZE; +off_t imgSize, iSize, fromBegin; +int Verbose, nameFlg, fileFlg, bufSize = IMGBUF_SIZE; extern char compiled[], compiledby[], compilehost[]; static void @@ -60,7 +61,7 @@ Usage() " Syntax: imgupd [options] [image_file]\n\n" "\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" @@ -69,65 +70,108 @@ Usage() } static int -EmptyStore(int img) +getFDtype(int fd) { + int type, flg = 0; + struct stat sb; + + if (fstat(fd, &sb) == -1) { + ESYSERR(0); + return -1; + } + if (S_ISREG(sb.st_mode)) + flg |= FD_TRUNC; + if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { + if (ioctl(fd, FIODTYPE, &type) == -1) { + ESYSERR(0); + return -1; + } + if (type & D_TAPE) + flg |= FD_TAPE; + else if (type & (D_DISK | D_MEM)) + flg |= FD_SEEK; + if (S_ISCHR(sb.st_mode) && !(type & D_TAPE)) + flg |= FD_CHR; + goto end; + } + errno ^= errno; + if (lseek(fd, 0, SEEK_CUR) == -1 && errno == ESPIPE) + flg |= FD_PIPE; + else + flg |= FD_SEEK; +end: + return flg; +} + +static int +EmptyStore(int img, int flg) +{ register int i; u_char buf[bufSize]; ssize_t wlen; VERB(1) printf("Erase store %s\n", imgName); - iSize = lseek(img, 0, SEEK_END); - if (iSize == -1) { - ESYSERR(0); - return -1; + if (!fromBegin && flg & FD_SEEK) { + 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 %zd bytes, " + "should be %d\n", i, wlen, bufSize); return -1; } else VERB(1) printf("+Written chunk #%d\n", i); - iSize = lseek(img, iSize, SEEK_SET); + if (flg & FD_SEEK) + iSize = lseek(img, iSize, SEEK_SET); return iSize; } static int -FillStore(int img, int fd) +FillStore(int img, int iflg, int fd, int fflg) { register int i, j; - u_char buf[bufSize]; - ssize_t rlen, wlen; + u_char *pos, buf[bufSize]; + ssize_t rlen, wlen, len; VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile); for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++, j++) { memset(buf, 0, sizeof buf); - rlen = read(fd, buf, sizeof buf); - if (rlen == -1) { - ESYSERR(0); - return -1; - } else if (!rlen) - break; - else - VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j); + for (len = bufSize, pos = buf; len > 0; len -= rlen, pos += rlen) { + rlen = read(fd, pos, len); + if (rlen == -1) { + ESYSERR(0); + return -1; + } else if (!rlen) + break; + else + VERB(1) printf("+Readed %zd 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 %zd bytes, " + "should be %u\n", i, wlen, bufSize); } else - VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i); + VERB(1) printf("+Written %zd bytes at chunk #%d\n", wlen, i); } return 0; @@ -139,7 +183,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, "hvRigts:f:")) != -1) switch (ch) { case 'f': strlcpy(imgName, optarg, sizeof imgName); @@ -156,10 +200,9 @@ main(int argc, char **argv) break; case 'g': m = 1; + case 'i': + fromBegin = 1; break; - case 'p': - bufSize = IMGBUF_SIZE2; - break; case 'R': R = 1; break; @@ -183,22 +226,33 @@ main(int argc, char **argv) ESYSERR(0); return 2; } else + fileFlg = getFDtype(fd); + + if (fileFlg & FD_SEEK) { iSize = lseek(fd, 0, SEEK_END); - if (!imgSize) - imgSize = E_ALIGN(iSize, bufSize); - if (iSize == -1 || iSize > imgSize) { + if (!imgSize) + imgSize = E_ALIGN(iSize, bufSize); + if (iSize == -1 || iSize > imgSize) { + close(fd); + EERROR(ENOSPC, "Error:: file size %llu is " + "greater from storage size %llu\n", + (unsigned long long) iSize, + (unsigned long long) imgSize); + return 2; + } else + lseek(fd, 0, SEEK_SET); + } + } else { + fd = STDIN_FILENO; + fileFlg = getFDtype(fd); + } + + if (!imgSize) { + if (fd > 2) close(fd); - EERROR(ENOSPC, "Error:: file size %llu is " - "greater from storage size %llu\n", - iSize, imgSize); - return 2; - } else - lseek(fd, 0, SEEK_SET); - } else if (!imgSize) { Usage(); return 1; - } else - fd = STDIN_FILENO; + } } else { /* GET */ if (argc) { strlcpy(imgFile, *argv, sizeof imgFile); @@ -207,16 +261,16 @@ main(int argc, char **argv) if (fd == -1) { ESYSERR(0); return 2; - } - } else if (!imgSize) { - Usage(); - return 1; - } else + } else + fileFlg = getFDtype(fd); + } else { fd = STDOUT_FILENO; + fileFlg = getFDtype(fd); + } } VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", - imgSize, imgName, argc ? imgFile : ""); + (unsigned long long) imgSize, imgName, argc ? imgFile : ""); if (!m) { /* open storage device */ @@ -226,7 +280,8 @@ main(int argc, char **argv) if (fd > 2) close(fd); return 3; - } + } else + nameFlg = getFDtype(img); } else { /* GET */ /* open storage device */ img = open(imgName, O_RDONLY); @@ -236,42 +291,56 @@ main(int argc, char **argv) close(fd); return 3; } else + nameFlg = getFDtype(img); + + if (nameFlg & FD_SEEK) { iSize = lseek(img, 0, SEEK_END); - if (!imgSize) - imgSize = E_ALIGN(iSize, bufSize); - if (iSize == -1 || iSize > imgSize) { + if (!imgSize) + imgSize = E_ALIGN(iSize, bufSize); + if (iSize == -1 || iSize > imgSize) { + if (fd > 2) + close(fd); + close(img); + EERROR(ENOSPC, "Error:: storage size %llu is " + "greater from file size %llu\n", + (unsigned long long) iSize, + (unsigned long long) imgSize); + return 3; + } else + lseek(img, 0, SEEK_SET); + } + + if (!imgSize) { if (fd > 2) close(fd); - close(img); - EERROR(ENOSPC, "Error:: storage size %llu is " - "greater from file size %llu\n", - iSize, imgSize); - return 3; - } else - lseek(img, 0, SEEK_SET); + Usage(); + return 1; + } } if (!m) { - if (EmptyStore(img) == -1) { + if (EmptyStore(img, nameFlg) == -1) { if (fd > 2) close(fd); close(img); return 3; } - if (FillStore(img, fd) == -1) { + if (FillStore(img, nameFlg, fd, fileFlg) == -1) { if (fd > 2) close(fd); close(img); return 4; } } else { /* GET */ - if (EmptyStore(fd) == -1) { - if (fd > 2) - close(fd); - close(img); - return 3; - } - if (FillStore(fd, img) == -1) { + iSize ^= iSize; + if (!(fileFlg & FD_PIPE)) + if (EmptyStore(fd, fileFlg) == -1) { + if (fd > 2) + close(fd); + close(img); + return 3; + } + if (FillStore(fd, fileFlg, img, nameFlg) == -1) { if (fd > 2) close(fd); close(img);