Annotation of embedtools/src/imgupd.c, revision 1.6.2.4
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.4 ! misho 6: * $Id: imgupd.c,v 1.6.2.3 2014/02/06 00:56:16 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.4 misho 112: u_char buf[bufSize];
1.2 misho 113: ssize_t rlen, wlen;
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.5 misho 120: rlen = read(fd, buf, bufSize);
1.2 misho 121: if (rlen == -1) {
122: ESYSERR(0);
123: return -1;
124: } else if (!rlen)
125: break;
126: else
127: VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
128:
1.6.2.3 misho 129: wlen = write(img, buf, bufSize);
1.2 misho 130: if (wlen == -1) {
131: ESYSERR(0);
132: return -1;
1.6.2.3 misho 133: } else if (wlen && wlen != bufSize) {
134: EERROR(EIO, "Error at chunk %d written %d bytes, "
135: "should be %u\n", i, wlen, bufSize);
1.2 misho 136: } else
137: VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
138: }
139:
140: return 0;
141: }
142:
143: int
144: main(int argc, char **argv)
145: {
1.4 misho 146: char ch, m = 0, R = 0;
1.2 misho 147: int fd, img, tr = 0;
148:
1.6.2.1 misho 149: while ((ch = getopt(argc, argv, "hvRipgts:f:")) != -1)
1.2 misho 150: switch (ch) {
151: case 'f':
152: strlcpy(imgName, optarg, sizeof imgName);
153: break;
154: case 's':
155: imgSize = strtoll(optarg, NULL, 0);
156: if (!imgSize) {
157: Usage();
158: return 1;
159: }
160: break;
161: case 't':
162: tr = O_TRUNC;
163: break;
1.4 misho 164: case 'p':
165: bufSize = IMGBUF_SIZE2;
166: break;
1.6.2.2 misho 167: case 'g':
168: m = 1;
1.6.2.1 misho 169: case 'i':
170: fromBegin = 1;
171: break;
1.4 misho 172: case 'R':
173: R = 1;
174: break;
1.2 misho 175: case 'v':
176: Verbose++;
177: break;
178: case 'h':
179: default:
180: Usage();
181: return 1;
182: }
183: argc -= optind;
184: argv += optind;
185:
1.3 misho 186: if (!m) {
187: if (argc) {
188: strlcpy(imgFile, *argv, sizeof imgFile);
189: /* open image file */
190: fd = open(imgFile, O_RDONLY);
191: if (fd == -1) {
192: ESYSERR(0);
193: return 2;
194: } else
195: iSize = lseek(fd, 0, SEEK_END);
196: if (!imgSize)
1.4 misho 197: imgSize = E_ALIGN(iSize, bufSize);
1.3 misho 198: if (iSize == -1 || iSize > imgSize) {
199: close(fd);
200: EERROR(ENOSPC, "Error:: file size %llu is "
201: "greater from storage size %llu\n",
202: iSize, imgSize);
203: return 2;
204: } else
205: lseek(fd, 0, SEEK_SET);
206: } else if (!imgSize) {
207: Usage();
208: return 1;
209: } else
210: fd = STDIN_FILENO;
211: } else { /* GET */
212: if (argc) {
213: strlcpy(imgFile, *argv, sizeof imgFile);
214: /* open image file */
215: fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
216: if (fd == -1) {
217: ESYSERR(0);
218: return 2;
219: }
220: } else if (!imgSize) {
221: Usage();
222: return 1;
223: } else
224: fd = STDOUT_FILENO;
225: }
226:
227: VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n",
228: imgSize, imgName, argc ? imgFile : "<stdin>");
229:
230: if (!m) {
231: /* open storage device */
232: img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
233: if (img == -1) {
1.2 misho 234: ESYSERR(0);
1.3 misho 235: if (fd > 2)
236: close(fd);
237: return 3;
238: }
239: } else { /* GET */
240: /* open storage device */
241: img = open(imgName, O_RDONLY);
242: if (img == -1) {
243: ESYSERR(0);
244: if (fd > 2)
245: close(fd);
246: return 3;
1.2 misho 247: } else
1.3 misho 248: iSize = lseek(img, 0, SEEK_END);
1.2 misho 249: if (!imgSize)
1.4 misho 250: imgSize = E_ALIGN(iSize, bufSize);
1.2 misho 251: if (iSize == -1 || iSize > imgSize) {
1.3 misho 252: if (fd > 2)
253: close(fd);
254: close(img);
255: EERROR(ENOSPC, "Error:: storage size %llu is "
256: "greater from file size %llu\n",
1.2 misho 257: iSize, imgSize);
1.3 misho 258: return 3;
1.2 misho 259: } else
1.3 misho 260: lseek(img, 0, SEEK_SET);
1.2 misho 261: }
262:
1.3 misho 263: if (!m) {
264: if (EmptyStore(img) == -1) {
265: if (fd > 2)
266: close(fd);
267: close(img);
268: return 3;
269: }
270: if (FillStore(img, fd) == -1) {
271: if (fd > 2)
272: close(fd);
273: close(img);
274: return 4;
275: }
276: } else { /* GET */
1.6.2.4 ! misho 277: iSize ^= iSize;
1.3 misho 278: if (EmptyStore(fd) == -1) {
279: if (fd > 2)
280: close(fd);
281: close(img);
282: return 3;
283: }
284: if (FillStore(fd, img) == -1) {
285: if (fd > 2)
286: close(fd);
287: close(img);
288: return 4;
289: }
1.2 misho 290: }
291:
292: close(img);
293: if (fd > 2)
294: close(fd);
1.4 misho 295:
296: if (R)
297: reboot(RB_AUTOBOOT);
1.2 misho 298: return 0;
299: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>