File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / imgupd.c
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Wed Jun 28 15:19:32 2017 UTC (6 years, 10 months ago) by misho
Branches: MAIN
CVS tags: tools3_0, tools2_9, tools2_8, TOOLS2_9, TOOLS2_8, TOOLS2_7, HEAD
ver 2.7

/*************************************************************************
 * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
 *  by Michael Pounov <misho@aitbg.com>
 *
 * $Author: misho $
 * $Id: imgupd.c,v 1.8 2017/06/28 15:19:32 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 <info@elwix.org>

Copyright 2004 - 2017
	by Michael Pounov <misho@elwix.org>.  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 <misho@elwix.org>
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"
#include "imgupd.h"


char imgName[PATH_MAX], imgFile[PATH_MAX];
off_t imgSize, iSize, fromBegin;
int Verbose, nameFlg, fileFlg, bufSize = IMGBUF_SIZE;
extern char compiled[], compiledby[], compilehost[];

static void
Usage()
{

	printf(	"IMGUPD is tool for management of images\n"
		"=== %s === %s@%s ===\n\n"
		"  Syntax: imgupd [options] [image_file]\n\n"
		"\t-v\t\tVerbose ...\n"
		"\t-R\t\tReboot system after complete\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 <size>\tStorage size (required for stdin)\n"
		"\t-f <devfile>\tStorage file name\n"
		"\n", compiled, compiledby, compilehost);
}

static int
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);

	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
		iSize ^= iSize;

	memset(buf, 0, sizeof buf);
	for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++)
		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);

	if (flg & FD_SEEK)
		iSize = lseek(img, iSize, SEEK_SET);
	return iSize;
}

static int
FillStore(int img, int iflg, int fd, int fflg)
{
	register int i, j;
	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);
		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, bufSize);
		if (wlen == -1) {
			ESYSERR(0);
			return -1;
		} 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 %zd bytes at chunk #%d\n", wlen, i);
	}

	return 0;
}

int
main(int argc, char **argv)
{
	char ch, m = 0, R = 0;
	int fd, img, tr = 0;

	while ((ch = getopt(argc, argv, "hvRigts:f:")) != -1)
		switch (ch) {
			case 'f':
				strlcpy(imgName, optarg, sizeof imgName);
				break;
			case 's':
				imgSize = strtoll(optarg, NULL, 0);
				if (!imgSize) {
					Usage();
					return 1;
				}
				break;
			case 't':
				tr = O_TRUNC;
				break;
			case 'g':
				m = 1;
			case 'i':
				fromBegin = 1;
				break;
			case 'R':
				R = 1;
				break;
			case 'v':
				Verbose++;
				break;
			case 'h':
			default:
				Usage();
				return 1;
		}
	argc -= optind;
	argv += optind;

	if (!m) {
		if (argc) {
			strlcpy(imgFile, *argv, sizeof imgFile);
			/* open image file */
			fd = open(imgFile, O_RDONLY);
			if (fd == -1) {
				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) {
					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);
			Usage();
			return 1;
		}
	} else {	/* GET */
		if (argc) {
			strlcpy(imgFile, *argv, sizeof imgFile);
			/* open image file */
			fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
			if (fd == -1) {
				ESYSERR(0);
				return 2;
			} else
				fileFlg = getFDtype(fd);
		} else {
			fd = STDOUT_FILENO;
			fileFlg = getFDtype(fd);
		}
	}

	VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
			(unsigned long long) imgSize, imgName, argc ? imgFile : "<stdin>");

	if (!m) {
		/* open storage device */
		img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
		if (img == -1) {
			ESYSERR(0);
			if (fd > 2)
				close(fd);
			return 3;
		} else
			nameFlg = getFDtype(img);
	} else {	/* GET */
		/* open storage device */
		img = open(imgName, O_RDONLY);
		if (img == -1) {
			ESYSERR(0);
			if (fd > 2)
				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 (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);
			Usage();
			return 1;
		}
	}

	if (!m) {
		if (EmptyStore(img, nameFlg) == -1) {
			if (fd > 2)
				close(fd);
			close(img);
			return 3;
		}
		if (FillStore(img, nameFlg, fd, fileFlg) == -1) {
			if (fd > 2)
				close(fd);
			close(img);
			return 4;
		}
	} else {	/* GET */
		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);
			return 4;
		}
	}

	close(img);
	if (fd > 2)
		close(fd);

	if (R)
		reboot(RB_AUTOBOOT);
	return 0;
}

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