File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / imgupd.c
Revision 1.3.2.1: download - view: text, annotated - select for diffs - revision graph
Wed Feb 5 22:44:13 2014 UTC (10 years, 5 months ago) by misho
Branches: tools2_2
Diff to: branchpoint 1.3: preferred, unified
add reboot opt

    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.3.2.1 2014/02/05 22:44:13 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: 
   48: 
   49: char imgName[PATH_MAX], imgFile[PATH_MAX];
   50: off_t imgSize, iSize;
   51: int Verbose;
   52: extern char compiled[], compiledby[], compilehost[];
   53: 
   54: static void
   55: Usage()
   56: {
   57: 
   58: 	printf(	"IMGUPD is tool for management of images\n"
   59: 		"=== %s === %s@%s ===\n\n"
   60: 		"  Syntax: imgupd [options] [image_file]\n\n"
   61: 		"\t-v\t\tVerbose ...\n"
   62: 		"\t-R\t\tReboot system after complete\n"
   63: 		"\t-g\t\tGet image from Storage\n"
   64: 		"\t-t\t\tTruncate Storage file name\n"
   65: 		"\t-s <size>\tStorage size (required for stdin)\n"
   66: 		"\t-f <devfile>\tStorage file name\n"
   67: 		"\n", compiled, compiledby, compilehost);
   68: }
   69: 
   70: static int
   71: EmptyStore(int img)
   72: {
   73: 	register int i;
   74: 	u_char buf[IMGBUF_SIZE];
   75: 	ssize_t wlen;
   76: 
   77: 	VERB(1) printf("Erase store %s\n", imgName);
   78: 
   79: 	iSize = lseek(img, 0, SEEK_END);
   80: 	if (iSize == -1) {
   81: 		ESYSERR(0);
   82: 		return -1;
   83: 	} else
   84: 		imgSize += E_ALIGN(iSize, IMGBUF_SIZE);
   85: 
   86: 	memset(buf, 0, sizeof buf);
   87: 	for (i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); i++)
   88: 		if ((wlen = write(img, buf, sizeof buf)) == -1 || 
   89: 				wlen != sizeof buf) {
   90: 			EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", 
   91: 					i, wlen, sizeof buf);
   92: 			return -1;
   93: 		} else
   94: 			VERB(1) printf("+Written chunk #%d\n", i);
   95: 
   96: 	iSize = lseek(img, iSize, SEEK_SET);
   97: 	return iSize;
   98: }
   99: 
  100: static int
  101: FillStore(int img, int fd)
  102: {
  103: 	register int i, j;
  104: 	u_char buf[IMGBUF_SIZE];
  105: 	ssize_t rlen, wlen;
  106: 
  107: 	VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
  108: 
  109: 	for (j = 0, i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); 
  110: 			i++, j++) {
  111: 		memset(buf, 0, sizeof buf);
  112: 		rlen = read(fd, buf, sizeof buf);
  113: 		if (rlen == -1) {
  114: 			ESYSERR(0);
  115: 			return -1;
  116: 		} else if (!rlen)
  117: 			break;
  118: 		else
  119: 			VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
  120: 
  121: 		wlen = write(img, buf, rlen);
  122: 		if (wlen == -1) {
  123: 			ESYSERR(0);
  124: 			return -1;
  125: 		} else if (!wlen || wlen != rlen) {
  126: 			EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", 
  127: 					rlen, wlen);
  128: 		} else
  129: 			VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
  130: 	}
  131: 
  132: 	return 0;
  133: }
  134: 
  135: int
  136: main(int argc, char **argv)
  137: {
  138: 	char ch, m = 0, R = 0;
  139: 	int fd, img, tr = 0;
  140: 
  141: 	while ((ch = getopt(argc, argv, "hvRgts:f:")) != -1)
  142: 		switch (ch) {
  143: 			case 'f':
  144: 				strlcpy(imgName, optarg, sizeof imgName);
  145: 				break;
  146: 			case 's':
  147: 				imgSize = strtoll(optarg, NULL, 0);
  148: 				if (!imgSize) {
  149: 					Usage();
  150: 					return 1;
  151: 				}
  152: 				break;
  153: 			case 't':
  154: 				tr = O_TRUNC;
  155: 				break;
  156: 			case 'g':
  157: 				m = 1;
  158: 				break;
  159: 			case 'R':
  160: 				R = 1;
  161: 				break;
  162: 			case 'v':
  163: 				Verbose++;
  164: 				break;
  165: 			case 'h':
  166: 			default:
  167: 				Usage();
  168: 				return 1;
  169: 		}
  170: 	argc -= optind;
  171: 	argv += optind;
  172: 
  173: 	if (!m) {
  174: 		if (argc) {
  175: 			strlcpy(imgFile, *argv, sizeof imgFile);
  176: 			/* open image file */
  177: 			fd = open(imgFile, O_RDONLY);
  178: 			if (fd == -1) {
  179: 				ESYSERR(0);
  180: 				return 2;
  181: 			} else
  182: 				iSize = lseek(fd, 0, SEEK_END);
  183: 			if (!imgSize)
  184: 				imgSize = E_ALIGN(iSize, IMGBUF_SIZE);
  185: 			if (iSize == -1 || iSize > imgSize) {
  186: 				close(fd);
  187: 				EERROR(ENOSPC, "Error:: file size %llu is "
  188: 						"greater from storage size %llu\n", 
  189: 						iSize, imgSize);
  190: 				return 2;
  191: 			} else
  192: 				lseek(fd, 0, SEEK_SET);
  193: 		} else if (!imgSize) {
  194: 			Usage();
  195: 			return 1;
  196: 		} else
  197: 			fd = STDIN_FILENO;
  198: 	} else {	/* GET */
  199: 		if (argc) {
  200: 			strlcpy(imgFile, *argv, sizeof imgFile);
  201: 			/* open image file */
  202: 			fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  203: 			if (fd == -1) {
  204: 				ESYSERR(0);
  205: 				return 2;
  206: 			}
  207: 		} else if (!imgSize) {
  208: 			Usage();
  209: 			return 1;
  210: 		} else
  211: 			fd = STDOUT_FILENO;
  212: 	}
  213: 
  214: 	VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
  215: 			imgSize, imgName, argc ? imgFile : "<stdin>");
  216: 
  217: 	if (!m) {
  218: 		/* open storage device */
  219: 		img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
  220: 		if (img == -1) {
  221: 			ESYSERR(0);
  222: 			if (fd > 2)
  223: 				close(fd);
  224: 			return 3;
  225: 		}
  226: 	} else {	/* GET */
  227: 		/* open storage device */
  228: 		img = open(imgName, O_RDONLY);
  229: 		if (img == -1) {
  230: 			ESYSERR(0);
  231: 			if (fd > 2)
  232: 				close(fd);
  233: 			return 3;
  234: 		} else
  235: 			iSize = lseek(img, 0, SEEK_END);
  236: 		if (!imgSize)
  237: 			imgSize = E_ALIGN(iSize, IMGBUF_SIZE);
  238: 		if (iSize == -1 || iSize > imgSize) {
  239: 			if (fd > 2)
  240: 				close(fd);
  241: 			close(img);
  242: 			EERROR(ENOSPC, "Error:: storage size %llu is "
  243: 					"greater from file size %llu\n", 
  244: 					iSize, imgSize);
  245: 			return 3;
  246: 		} else
  247: 			lseek(img, 0, SEEK_SET);
  248: 	}
  249: 
  250: 	if (!m) {
  251: 		if (EmptyStore(img) == -1) {
  252: 			if (fd > 2)
  253: 				close(fd);
  254: 			close(img);
  255: 			return 3;
  256: 		}
  257: 		if (FillStore(img, fd) == -1) {
  258: 			if (fd > 2)
  259: 				close(fd);
  260: 			close(img);
  261: 			return 4;
  262: 		}
  263: 	} else {	/* GET */
  264: 		if (EmptyStore(fd) == -1) {
  265: 			if (fd > 2)
  266: 				close(fd);
  267: 			close(img);
  268: 			return 3;
  269: 		}
  270: 		if (FillStore(fd, img) == -1) {
  271: 			if (fd > 2)
  272: 				close(fd);
  273: 			close(img);
  274: 			return 4;
  275: 		}
  276: 	}
  277: 
  278: 	close(img);
  279: 	if (fd > 2)
  280: 		close(fd);
  281: 
  282: 	if (R)
  283: 		reboot(RB_AUTOBOOT);
  284: 	return 0;
  285: }

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