--- embedtools/src/imgupd.c 2014/02/05 13:57:01 1.1.2.3 +++ embedtools/src/imgupd.c 2014/02/05 15:44:06 1.2 @@ -1,8 +1,53 @@ +/************************************************************************* + * (C) 2014 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: imgupd.c,v 1.2 2014/02/05 15:44:06 misho Exp $ + * + ************************************************************************* +The ELWIX and AITNET software is distributed under the following +terms: + +All of the documentation and software included in the ELWIX and AITNET +Releases is copyrighted by ELWIX - Sofia/Bulgaria + +Copyright 2004 - 2014 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" char imgName[PATH_MAX], imgFile[PATH_MAX]; -off_t imgSize, fSize, iSize; +off_t imgSize, iSize; int Verbose; extern char compiled[], compiledby[], compilehost[]; @@ -15,7 +60,7 @@ Usage() " Syntax: imgupd [options] \n\n" "\t-v\t\tVerbose ...\n" "\t-t\t\tTruncate Storage file name\n" - "\t-s \tStorage image file to size\n" + "\t-s \tStorage size (required for stdin)\n" "\t-f \tStorage file name\n" "\n", compiled, compiledby, compilehost); } @@ -24,7 +69,7 @@ static int EmptyStore(int img) { register int i; - u_char buf[IMGBLOCK_SIZE]; + u_char buf[IMGBUF_SIZE]; ssize_t wlen; VERB(1) printf("Erase store %s\n", imgName); @@ -34,10 +79,10 @@ EmptyStore(int img) ESYSERR(0); return -1; } else - imgSize += E_ALIGN(iSize, IMGBLOCK_SIZE); + imgSize += E_ALIGN(iSize, IMGBUF_SIZE); memset(buf, 0, sizeof buf); - for (i = howmany(iSize, IMGBLOCK_SIZE); i < howmany(imgSize, IMGBLOCK_SIZE); i++) + for (i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); 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", @@ -54,14 +99,12 @@ static int FillStore(int img, int fd) { register int i, j; - u_char buf[IMGBLOCK_SIZE]; + u_char buf[IMGBUF_SIZE]; ssize_t rlen, wlen; VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile); - iSize = lseek(img, 0, SEEK_CUR); - printf("n_%lld\n", iSize); - for (j = 0, i = howmany(iSize, IMGBLOCK_SIZE); i < howmany(imgSize, IMGBLOCK_SIZE); + for (j = 0, i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); i++, j++) { memset(buf, 0, sizeof buf); rlen = read(fd, buf, sizeof buf); @@ -118,51 +161,60 @@ main(int argc, char **argv) } argc -= optind; argv += optind; - if (!argc || !*imgName || !imgSize) { + + if (argc) { + strlcpy(imgFile, *argv, sizeof imgFile); + /* open image file */ + fd = open(imgFile, O_RDONLY); + if (fd == -1) { + ESYSERR(0); + return 2; + } else + iSize = lseek(fd, 0, SEEK_END); + if (!imgSize) + imgSize = E_ALIGN(iSize, IMGBUF_SIZE); + if (iSize == -1 || iSize > imgSize) { + 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 - strlcpy(imgFile, *argv, sizeof imgFile); + fd = STDIN_FILENO; - VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", imgSize, imgName, imgFile); + VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", + imgSize, imgName, argc ? imgFile : ""); - /* open image file */ - fd = open(imgFile, O_RDONLY); - if (fd == -1) { - ESYSERR(0); - return 2; - } else - fSize = lseek(fd, 0, SEEK_END); - if (fSize == -1 || fSize > imgSize) { - close(fd); - EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n", - fSize, imgSize); - return 2; - } else - lseek(fd, 0, SEEK_SET); - /* open storage device */ img = open(imgName, O_RDWR | O_CREAT | tr, 0644); if (img == -1) { ESYSERR(0); - close(fd); + if (fd > 2) + close(fd); return 3; } if (EmptyStore(img) == -1) { - close(fd); + if (fd > 2) + close(fd); close(img); unlink(imgName); return 3; } if (FillStore(img, fd) == -1) { - close(fd); + if (fd > 2) + close(fd); close(img); unlink(imgName); return 4; } close(img); - close(fd); + if (fd > 2) + close(fd); return 0; }