File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / imgupd.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Thu Feb 6 15:32:11 2014 UTC (10 years, 4 months ago) by misho
Branches: MAIN
CVS tags: tools2_7, tools2_6, TOOLS2_6, TOOLS2_5, HEAD
version 2.5

    1: /*************************************************************************
    2:  * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
    3:  *  by Michael Pounov <misho@aitbg.com>
    4:  *
    5:  * $Author: misho $
    6:  * $Id: imgupd.c,v 1.7 2014/02/06 15:32:11 misho Exp $
    7:  *
    8:  *************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2014
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "imgupd.h"
   48: 
   49: 
   50: char imgName[PATH_MAX], imgFile[PATH_MAX];
   51: off_t imgSize, iSize, fromBegin;
   52: int Verbose, nameFlg, fileFlg, bufSize = IMGBUF_SIZE;
   53: extern char compiled[], compiledby[], compilehost[];
   54: 
   55: static void
   56: Usage()
   57: {
   58: 
   59: 	printf(	"IMGUPD is tool for management of images\n"
   60: 		"=== %s === %s@%s ===\n\n"
   61: 		"  Syntax: imgupd [options] [image_file]\n\n"
   62: 		"\t-v\t\tVerbose ...\n"
   63: 		"\t-R\t\tReboot system after complete\n"
   64: 		"\t-i\t\tStart fill storage from begin\n"
   65: 		"\t-g\t\tGet image from Storage\n"
   66: 		"\t-t\t\tTruncate Storage file name\n"
   67: 		"\t-s <size>\tStorage size (required for stdin)\n"
   68: 		"\t-f <devfile>\tStorage file name\n"
   69: 		"\n", compiled, compiledby, compilehost);
   70: }
   71: 
   72: static int
   73: getFDtype(int fd)
   74: {
   75: 	int type, flg = 0;
   76: 	struct stat sb;
   77: 
   78: 	if (fstat(fd, &sb) == -1) {
   79: 		ESYSERR(0);
   80: 		return -1;
   81: 	}
   82: 	if (S_ISREG(sb.st_mode))
   83: 		flg |= FD_TRUNC;
   84: 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
   85: 		if (ioctl(fd, FIODTYPE, &type) == -1) {
   86: 			ESYSERR(0);
   87: 			return -1;
   88: 		}
   89: 		if (type & D_TAPE)
   90: 			flg |= FD_TAPE;
   91: 		else if (type & (D_DISK | D_MEM))
   92: 			flg |= FD_SEEK;
   93: 		if (S_ISCHR(sb.st_mode) && !(type & D_TAPE))
   94: 			flg |= FD_CHR;
   95: 		goto end;
   96: 	}
   97: 	errno ^= errno;
   98: 	if (lseek(fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
   99: 		flg |= FD_PIPE;
  100: 	else
  101: 		flg |= FD_SEEK;
  102: end:
  103: 	return flg;
  104: }
  105: 
  106: static int
  107: EmptyStore(int img, int flg)
  108: {
  109: 	register int i;
  110: 	u_char buf[bufSize];
  111: 	ssize_t wlen;
  112: 
  113: 	VERB(1) printf("Erase store %s\n", imgName);
  114: 
  115: 	if (!fromBegin && flg & FD_SEEK) {
  116: 		iSize = lseek(img, 0, SEEK_END);
  117: 		if (iSize == -1) {
  118: 			ESYSERR(0);
  119: 			return -1;
  120: 		} else
  121: 			imgSize += E_ALIGN(iSize, bufSize);
  122: 	} else
  123: 		iSize ^= iSize;
  124: 
  125: 	memset(buf, 0, sizeof buf);
  126: 	for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++)
  127: 		if ((wlen = write(img, buf, bufSize)) == -1 || 
  128: 				(wlen && wlen != bufSize)) {
  129: 			if (wlen == -1)
  130: 				ESYSERR(0);
  131: 			else
  132: 				EERROR(EIO, "Error at chunk %d init %d bytes, "
  133: 						"should be %u\n", i, wlen, bufSize);
  134: 			return -1;
  135: 		} else
  136: 			VERB(1) printf("+Written chunk #%d\n", i);
  137: 
  138: 	if (flg & FD_SEEK)
  139: 		iSize = lseek(img, iSize, SEEK_SET);
  140: 	return iSize;
  141: }
  142: 
  143: static int
  144: FillStore(int img, int iflg, int fd, int fflg)
  145: {
  146: 	register int i, j;
  147: 	u_char *pos, buf[bufSize];
  148: 	ssize_t rlen, wlen, len;
  149: 
  150: 	VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
  151: 
  152: 	for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); 
  153: 			i++, j++) {
  154: 		memset(buf, 0, sizeof buf);
  155: 		for (len = bufSize, pos = buf; len > 0; len -= rlen, pos += rlen) {
  156: 			rlen = read(fd, pos, len);
  157: 			if (rlen == -1) {
  158: 				ESYSERR(0);
  159: 				return -1;
  160: 			} else if (!rlen)
  161: 				break;
  162: 			else
  163: 				VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
  164: 		}
  165: 
  166: 		wlen = write(img, buf, bufSize);
  167: 		if (wlen == -1) {
  168: 			ESYSERR(0);
  169: 			return -1;
  170: 		} else if (wlen && wlen != bufSize) {
  171: 			EERROR(EIO, "Error at chunk %d written %d bytes, "
  172: 					"should be %u\n", i, wlen, bufSize);
  173: 		} else
  174: 			VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
  175: 	}
  176: 
  177: 	return 0;
  178: }
  179: 
  180: int
  181: main(int argc, char **argv)
  182: {
  183: 	char ch, m = 0, R = 0;
  184: 	int fd, img, tr = 0;
  185: 
  186: 	while ((ch = getopt(argc, argv, "hvRigts:f:")) != -1)
  187: 		switch (ch) {
  188: 			case 'f':
  189: 				strlcpy(imgName, optarg, sizeof imgName);
  190: 				break;
  191: 			case 's':
  192: 				imgSize = strtoll(optarg, NULL, 0);
  193: 				if (!imgSize) {
  194: 					Usage();
  195: 					return 1;
  196: 				}
  197: 				break;
  198: 			case 't':
  199: 				tr = O_TRUNC;
  200: 				break;
  201: 			case 'g':
  202: 				m = 1;
  203: 			case 'i':
  204: 				fromBegin = 1;
  205: 				break;
  206: 			case 'R':
  207: 				R = 1;
  208: 				break;
  209: 			case 'v':
  210: 				Verbose++;
  211: 				break;
  212: 			case 'h':
  213: 			default:
  214: 				Usage();
  215: 				return 1;
  216: 		}
  217: 	argc -= optind;
  218: 	argv += optind;
  219: 
  220: 	if (!m) {
  221: 		if (argc) {
  222: 			strlcpy(imgFile, *argv, sizeof imgFile);
  223: 			/* open image file */
  224: 			fd = open(imgFile, O_RDONLY);
  225: 			if (fd == -1) {
  226: 				ESYSERR(0);
  227: 				return 2;
  228: 			} else
  229: 				fileFlg = getFDtype(fd);
  230: 
  231: 			if (fileFlg & FD_SEEK) {
  232: 				iSize = lseek(fd, 0, SEEK_END);
  233: 				if (!imgSize)
  234: 					imgSize = E_ALIGN(iSize, bufSize);
  235: 				if (iSize == -1 || iSize > imgSize) {
  236: 					close(fd);
  237: 					EERROR(ENOSPC, "Error:: file size %llu is "
  238: 							"greater from storage size %llu\n", 
  239: 							iSize, imgSize);
  240: 					return 2;
  241: 				} else
  242: 					lseek(fd, 0, SEEK_SET);
  243: 			}
  244: 		} else {
  245: 			fd = STDIN_FILENO;
  246: 			fileFlg = getFDtype(fd);
  247: 		}
  248: 
  249: 		if (!imgSize) {
  250: 			if (fd > 2)
  251: 				close(fd);
  252: 			Usage();
  253: 			return 1;
  254: 		}
  255: 	} else {	/* GET */
  256: 		if (argc) {
  257: 			strlcpy(imgFile, *argv, sizeof imgFile);
  258: 			/* open image file */
  259: 			fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  260: 			if (fd == -1) {
  261: 				ESYSERR(0);
  262: 				return 2;
  263: 			} else
  264: 				fileFlg = getFDtype(fd);
  265: 		} else {
  266: 			fd = STDOUT_FILENO;
  267: 			fileFlg = getFDtype(fd);
  268: 		}
  269: 	}
  270: 
  271: 	VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
  272: 			imgSize, imgName, argc ? imgFile : "<stdin>");
  273: 
  274: 	if (!m) {
  275: 		/* open storage device */
  276: 		img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
  277: 		if (img == -1) {
  278: 			ESYSERR(0);
  279: 			if (fd > 2)
  280: 				close(fd);
  281: 			return 3;
  282: 		} else
  283: 			nameFlg = getFDtype(img);
  284: 	} else {	/* GET */
  285: 		/* open storage device */
  286: 		img = open(imgName, O_RDONLY);
  287: 		if (img == -1) {
  288: 			ESYSERR(0);
  289: 			if (fd > 2)
  290: 				close(fd);
  291: 			return 3;
  292: 		} else
  293: 			nameFlg = getFDtype(img);
  294: 
  295: 		if (nameFlg & FD_SEEK) {
  296: 			iSize = lseek(img, 0, SEEK_END);
  297: 			if (!imgSize)
  298: 				imgSize = E_ALIGN(iSize, bufSize);
  299: 			if (iSize == -1 || iSize > imgSize) {
  300: 				if (fd > 2)
  301: 					close(fd);
  302: 				close(img);
  303: 				EERROR(ENOSPC, "Error:: storage size %llu is "
  304: 						"greater from file size %llu\n", 
  305: 						iSize, imgSize);
  306: 				return 3;
  307: 			} else
  308: 				lseek(img, 0, SEEK_SET);
  309: 		}
  310: 
  311: 		if (!imgSize) {
  312: 			if (fd > 2)
  313: 				close(fd);
  314: 			Usage();
  315: 			return 1;
  316: 		}
  317: 	}
  318: 
  319: 	if (!m) {
  320: 		if (EmptyStore(img, nameFlg) == -1) {
  321: 			if (fd > 2)
  322: 				close(fd);
  323: 			close(img);
  324: 			return 3;
  325: 		}
  326: 		if (FillStore(img, nameFlg, fd, fileFlg) == -1) {
  327: 			if (fd > 2)
  328: 				close(fd);
  329: 			close(img);
  330: 			return 4;
  331: 		}
  332: 	} else {	/* GET */
  333: 		iSize ^= iSize;
  334: 		if (!(fileFlg & FD_PIPE))
  335: 			if (EmptyStore(fd, fileFlg) == -1) {
  336: 				if (fd > 2)
  337: 					close(fd);
  338: 				close(img);
  339: 				return 3;
  340: 			}
  341: 		if (FillStore(fd, fileFlg, img, nameFlg) == -1) {
  342: 			if (fd > 2)
  343: 				close(fd);
  344: 			close(img);
  345: 			return 4;
  346: 		}
  347: 	}
  348: 
  349: 	close(img);
  350: 	if (fd > 2)
  351: 		close(fd);
  352: 
  353: 	if (R)
  354: 		reboot(RB_AUTOBOOT);
  355: 	return 0;
  356: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>