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.2 2014/02/05 15:44:06 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-t\t\tTruncate Storage file name\n"
63: "\t-s <size>\tStorage size (required for stdin)\n"
64: "\t-f <devfile>\tStorage file name\n"
65: "\n", compiled, compiledby, compilehost);
66: }
67:
68: static int
69: EmptyStore(int img)
70: {
71: register int i;
72: u_char buf[IMGBUF_SIZE];
73: ssize_t wlen;
74:
75: VERB(1) printf("Erase store %s\n", imgName);
76:
77: iSize = lseek(img, 0, SEEK_END);
78: if (iSize == -1) {
79: ESYSERR(0);
80: return -1;
81: } else
82: imgSize += E_ALIGN(iSize, IMGBUF_SIZE);
83:
84: memset(buf, 0, sizeof buf);
85: for (i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); i++)
86: if ((wlen = write(img, buf, sizeof buf)) == -1 ||
87: wlen != sizeof buf) {
88: EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n",
89: i, wlen, sizeof buf);
90: return -1;
91: } else
92: VERB(1) printf("+Written chunk #%d\n", i);
93:
94: iSize = lseek(img, iSize, SEEK_SET);
95: return iSize;
96: }
97:
98: static int
99: FillStore(int img, int fd)
100: {
101: register int i, j;
102: u_char buf[IMGBUF_SIZE];
103: ssize_t rlen, wlen;
104:
105: VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
106:
107: for (j = 0, i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE);
108: i++, j++) {
109: memset(buf, 0, sizeof buf);
110: rlen = read(fd, buf, sizeof buf);
111: if (rlen == -1) {
112: ESYSERR(0);
113: return -1;
114: } else if (!rlen)
115: break;
116: else
117: VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
118:
119: wlen = write(img, buf, rlen);
120: if (wlen == -1) {
121: ESYSERR(0);
122: return -1;
123: } else if (!wlen || wlen != rlen) {
124: EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n",
125: rlen, wlen);
126: } else
127: VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
128: }
129:
130: return 0;
131: }
132:
133: int
134: main(int argc, char **argv)
135: {
136: char ch;
137: int fd, img, tr = 0;
138:
139: while ((ch = getopt(argc, argv, "hvts:f:")) != -1)
140: switch (ch) {
141: case 'f':
142: strlcpy(imgName, optarg, sizeof imgName);
143: break;
144: case 's':
145: imgSize = strtoll(optarg, NULL, 0);
146: if (!imgSize) {
147: Usage();
148: return 1;
149: }
150: break;
151: case 't':
152: tr = O_TRUNC;
153: break;
154: case 'v':
155: Verbose++;
156: break;
157: case 'h':
158: default:
159: Usage();
160: return 1;
161: }
162: argc -= optind;
163: argv += optind;
164:
165: if (argc) {
166: strlcpy(imgFile, *argv, sizeof imgFile);
167: /* open image file */
168: fd = open(imgFile, O_RDONLY);
169: if (fd == -1) {
170: ESYSERR(0);
171: return 2;
172: } else
173: iSize = lseek(fd, 0, SEEK_END);
174: if (!imgSize)
175: imgSize = E_ALIGN(iSize, IMGBUF_SIZE);
176: if (iSize == -1 || iSize > imgSize) {
177: close(fd);
178: EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n",
179: iSize, imgSize);
180: return 2;
181: } else
182: lseek(fd, 0, SEEK_SET);
183: } else if (!imgSize) {
184: Usage();
185: return 1;
186: } else
187: fd = STDIN_FILENO;
188:
189: VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n",
190: imgSize, imgName, argc ? imgFile : "<stdin>");
191:
192: /* open storage device */
193: img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
194: if (img == -1) {
195: ESYSERR(0);
196: if (fd > 2)
197: close(fd);
198: return 3;
199: }
200: if (EmptyStore(img) == -1) {
201: if (fd > 2)
202: close(fd);
203: close(img);
204: unlink(imgName);
205: return 3;
206: }
207:
208: if (FillStore(img, fd) == -1) {
209: if (fd > 2)
210: close(fd);
211: close(img);
212: unlink(imgName);
213: return 4;
214: }
215:
216: close(img);
217: if (fd > 2)
218: close(fd);
219: return 0;
220: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>