Annotation of embedtools/src/imgupd.c, revision 1.6.2.5
1.2 misho 1: /*************************************************************************
2: * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
1.6.2.5 ! misho 6: * $Id: imgupd.c,v 1.6.2.4 2014/02/06 01:23:56 misho Exp $
1.2 misho 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];
1.6.2.1 misho 50: off_t imgSize, iSize, fromBegin;
1.4 misho 51: int Verbose, bufSize = IMGBUF_SIZE;
1.2 misho 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"
1.3 misho 60: " Syntax: imgupd [options] [image_file]\n\n"
1.2 misho 61: "\t-v\t\tVerbose ...\n"
1.4 misho 62: "\t-R\t\tReboot system after complete\n"
63: "\t-p\t\tPipe suitable transfer on little chunks\n"
1.6.2.1 misho 64: "\t-i\t\tStart fill storage from begin\n"
1.3 misho 65: "\t-g\t\tGet image from Storage\n"
1.2 misho 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: EmptyStore(int img)
74: {
75: register int i;
1.4 misho 76: u_char buf[bufSize];
1.2 misho 77: ssize_t wlen;
78:
79: VERB(1) printf("Erase store %s\n", imgName);
80:
1.6.2.1 misho 81: if (!fromBegin) {
82: iSize = lseek(img, 0, SEEK_END);
83: if (iSize == -1) {
84: ESYSERR(0);
85: return -1;
86: } else
87: imgSize += E_ALIGN(iSize, bufSize);
1.2 misho 88: } else
1.6.2.1 misho 89: iSize ^= iSize;
1.2 misho 90:
91: memset(buf, 0, sizeof buf);
1.4 misho 92: for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++)
1.5 misho 93: if ((wlen = write(img, buf, bufSize)) == -1 ||
94: (wlen && wlen != bufSize)) {
1.6.2.4 misho 95: if (wlen == -1)
96: ESYSERR(0);
97: else
98: EERROR(EIO, "Error at chunk %d init %d bytes, "
99: "should be %u\n", i, wlen, bufSize);
1.2 misho 100: return -1;
101: } else
102: VERB(1) printf("+Written chunk #%d\n", i);
103:
104: iSize = lseek(img, iSize, SEEK_SET);
105: return iSize;
106: }
107:
108: static int
109: FillStore(int img, int fd)
110: {
111: register int i, j;
1.6.2.5 ! misho 112: u_char *pos, buf[bufSize];
! 113: ssize_t rlen, wlen, len;
1.2 misho 114:
115: VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
116:
1.4 misho 117: for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize);
1.2 misho 118: i++, j++) {
119: memset(buf, 0, sizeof buf);
1.6.2.5 ! misho 120: for (len = bufSize, pos = buf; len > 0; len -= rlen, pos += rlen) {
! 121: rlen = read(fd, pos, len);
! 122: if (rlen == -1) {
! 123: ESYSERR(0);
! 124: return -1;
! 125: } else if (!rlen)
! 126: break;
! 127: else
! 128: VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
! 129: }
1.2 misho 130:
1.6.2.3 misho 131: wlen = write(img, buf, bufSize);
1.2 misho 132: if (wlen == -1) {
133: ESYSERR(0);
134: return -1;
1.6.2.3 misho 135: } else if (wlen && wlen != bufSize) {
136: EERROR(EIO, "Error at chunk %d written %d bytes, "
137: "should be %u\n", i, wlen, bufSize);
1.2 misho 138: } else
139: VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
140: }
141:
142: return 0;
143: }
144:
145: int
146: main(int argc, char **argv)
147: {
1.4 misho 148: char ch, m = 0, R = 0;
1.2 misho 149: int fd, img, tr = 0;
150:
1.6.2.1 misho 151: while ((ch = getopt(argc, argv, "hvRipgts:f:")) != -1)
1.2 misho 152: switch (ch) {
153: case 'f':
154: strlcpy(imgName, optarg, sizeof imgName);
155: break;
156: case 's':
157: imgSize = strtoll(optarg, NULL, 0);
158: if (!imgSize) {
159: Usage();
160: return 1;
161: }
162: break;
163: case 't':
164: tr = O_TRUNC;
165: break;
1.4 misho 166: case 'p':
167: bufSize = IMGBUF_SIZE2;
168: break;
1.6.2.2 misho 169: case 'g':
170: m = 1;
1.6.2.1 misho 171: case 'i':
172: fromBegin = 1;
173: break;
1.4 misho 174: case 'R':
175: R = 1;
176: break;
1.2 misho 177: case 'v':
178: Verbose++;
179: break;
180: case 'h':
181: default:
182: Usage();
183: return 1;
184: }
185: argc -= optind;
186: argv += optind;
187:
1.3 misho 188: if (!m) {
189: if (argc) {
190: strlcpy(imgFile, *argv, sizeof imgFile);
191: /* open image file */
192: fd = open(imgFile, O_RDONLY);
193: if (fd == -1) {
194: ESYSERR(0);
195: return 2;
196: } else
197: iSize = lseek(fd, 0, SEEK_END);
198: if (!imgSize)
1.4 misho 199: imgSize = E_ALIGN(iSize, bufSize);
1.3 misho 200: if (iSize == -1 || iSize > imgSize) {
201: close(fd);
202: EERROR(ENOSPC, "Error:: file size %llu is "
203: "greater from storage size %llu\n",
204: iSize, imgSize);
205: return 2;
206: } else
207: lseek(fd, 0, SEEK_SET);
208: } else if (!imgSize) {
209: Usage();
210: return 1;
211: } else
212: fd = STDIN_FILENO;
213: } else { /* GET */
214: if (argc) {
215: strlcpy(imgFile, *argv, sizeof imgFile);
216: /* open image file */
217: fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
218: if (fd == -1) {
219: ESYSERR(0);
220: return 2;
221: }
222: } else if (!imgSize) {
223: Usage();
224: return 1;
225: } else
226: fd = STDOUT_FILENO;
227: }
228:
229: VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n",
230: imgSize, imgName, argc ? imgFile : "<stdin>");
231:
232: if (!m) {
233: /* open storage device */
234: img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
235: if (img == -1) {
1.2 misho 236: ESYSERR(0);
1.3 misho 237: if (fd > 2)
238: close(fd);
239: return 3;
240: }
241: } else { /* GET */
242: /* open storage device */
243: img = open(imgName, O_RDONLY);
244: if (img == -1) {
245: ESYSERR(0);
246: if (fd > 2)
247: close(fd);
248: return 3;
1.2 misho 249: } else
1.3 misho 250: iSize = lseek(img, 0, SEEK_END);
1.2 misho 251: if (!imgSize)
1.4 misho 252: imgSize = E_ALIGN(iSize, bufSize);
1.2 misho 253: if (iSize == -1 || iSize > imgSize) {
1.3 misho 254: if (fd > 2)
255: close(fd);
256: close(img);
257: EERROR(ENOSPC, "Error:: storage size %llu is "
258: "greater from file size %llu\n",
1.2 misho 259: iSize, imgSize);
1.3 misho 260: return 3;
1.2 misho 261: } else
1.3 misho 262: lseek(img, 0, SEEK_SET);
1.2 misho 263: }
264:
1.3 misho 265: if (!m) {
266: if (EmptyStore(img) == -1) {
267: if (fd > 2)
268: close(fd);
269: close(img);
270: return 3;
271: }
272: if (FillStore(img, fd) == -1) {
273: if (fd > 2)
274: close(fd);
275: close(img);
276: return 4;
277: }
278: } else { /* GET */
1.6.2.4 misho 279: iSize ^= iSize;
1.3 misho 280: if (EmptyStore(fd) == -1) {
281: if (fd > 2)
282: close(fd);
283: close(img);
284: return 3;
285: }
286: if (FillStore(fd, img) == -1) {
287: if (fd > 2)
288: close(fd);
289: close(img);
290: return 4;
291: }
1.2 misho 292: }
293:
294: close(img);
295: if (fd > 2)
296: close(fd);
1.4 misho 297:
298: if (R)
299: reboot(RB_AUTOBOOT);
1.2 misho 300: return 0;
301: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>