version 1.1.2.2, 2014/02/05 12:59:55
|
version 1.6, 2014/02/05 23:31:55
|
Line 1
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com> |
|
* by Michael Pounov <misho@aitbg.com> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
************************************************************************* |
|
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 - 2014 |
|
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 "global.h" |
|
|
|
|
char imgName[PATH_MAX], imgFile[PATH_MAX]; |
char imgName[PATH_MAX], imgFile[PATH_MAX]; |
off_t imgSize, fSize, iSize; | off_t imgSize, iSize; |
int Verbose; | int Verbose, bufSize = IMGBUF_SIZE; |
extern char compiled[], compiledby[], compilehost[]; |
extern char compiled[], compiledby[], compilehost[]; |
|
|
static void |
static void |
Line 12 Usage()
|
Line 57 Usage()
|
|
|
printf( "IMGUPD is tool for management of images\n" |
printf( "IMGUPD is tool for management of images\n" |
"=== %s === %s@%s ===\n\n" |
"=== %s === %s@%s ===\n\n" |
" Syntax: imgupd [options] <image_file>\n\n" | " Syntax: imgupd [options] [image_file]\n\n" |
"\t-v\t\tVerbose ...\n" |
"\t-v\t\tVerbose ...\n" |
"\t-s <size>\tStorage size\n" | "\t-R\t\tReboot system after complete\n" |
| "\t-p\t\tPipe suitable transfer on little chunks\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" |
"\t-f <devfile>\tStorage file name\n" |
"\n", compiled, compiledby, compilehost); |
"\n", compiled, compiledby, compilehost); |
} |
} |
Line 23 static int
|
Line 72 static int
|
EmptyStore(int img) |
EmptyStore(int img) |
{ |
{ |
register int i; |
register int i; |
u_char buf[IMGBLOCK_SIZE]; | u_char buf[bufSize]; |
ssize_t wlen; |
ssize_t wlen; |
|
|
VERB(1) printf("Erase store %s\n", imgName); |
VERB(1) printf("Erase store %s\n", imgName); |
|
|
|
iSize = lseek(img, 0, SEEK_END); |
|
if (iSize == -1) { |
|
ESYSERR(0); |
|
return -1; |
|
} else |
|
imgSize += E_ALIGN(iSize, bufSize); |
|
|
memset(buf, 0, sizeof buf); |
memset(buf, 0, sizeof buf); |
for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++) | for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++) |
if ((wlen = write(img, buf, sizeof buf)) == -1 || | if ((wlen = write(img, buf, bufSize)) == -1 || |
wlen != sizeof buf) { | (wlen && wlen != bufSize)) { |
EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", | EERROR(EIO, "Error at chunk %d init %d bytes, " |
i, wlen, sizeof buf); | "should be %u\n", i, wlen, bufSize); |
return -1; |
return -1; |
} else |
} else |
VERB(1) printf("+Written chunk #%d\n", i); |
VERB(1) printf("+Written chunk #%d\n", i); |
|
|
iSize = lseek(img, 0, SEEK_END); | iSize = lseek(img, iSize, SEEK_SET); |
return iSize; |
return iSize; |
} |
} |
|
|
static int |
static int |
FillStore(int img, int fd) |
FillStore(int img, int fd) |
{ |
{ |
register int i; | register int i, j; |
u_char buf[IMGBLOCK_SIZE]; | u_char buf[bufSize]; |
ssize_t rlen, wlen; |
ssize_t rlen, wlen; |
|
|
VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile); |
VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile); |
|
|
for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++) { | for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); |
| i++, j++) { |
memset(buf, 0, sizeof buf); |
memset(buf, 0, sizeof buf); |
rlen = read(fd, buf, sizeof buf); | rlen = read(fd, buf, bufSize); |
if (rlen == -1) { |
if (rlen == -1) { |
ESYSERR(0); |
ESYSERR(0); |
return -1; |
return -1; |
} else if (!rlen) |
} else if (!rlen) |
break; |
break; |
else |
else |
VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, i); | VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j); |
|
|
wlen = write(img, buf, rlen); |
wlen = write(img, buf, rlen); |
if (wlen == -1) { |
if (wlen == -1) { |
ESYSERR(0); |
ESYSERR(0); |
return -1; |
return -1; |
} else if (!wlen || wlen != rlen) { | } else if (wlen && wlen != rlen) { |
EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", |
EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", |
rlen, wlen); |
rlen, wlen); |
} else |
} else |
Line 79 FillStore(int img, int fd)
|
Line 136 FillStore(int img, int fd)
|
int |
int |
main(int argc, char **argv) |
main(int argc, char **argv) |
{ |
{ |
char ch; | char ch, m = 0, R = 0; |
int fd, img; | int fd, img, tr = 0; |
|
|
while ((ch = getopt(argc, argv, "hvs:f:")) != -1) | while ((ch = getopt(argc, argv, "hvRpgts:f:")) != -1) |
switch (ch) { |
switch (ch) { |
case 'f': |
case 'f': |
strlcpy(imgName, optarg, sizeof imgName); |
strlcpy(imgName, optarg, sizeof imgName); |
Line 94 main(int argc, char **argv)
|
Line 151 main(int argc, char **argv)
|
return 1; |
return 1; |
} |
} |
break; |
break; |
|
case 't': |
|
tr = O_TRUNC; |
|
break; |
|
case 'g': |
|
m = 1; |
|
break; |
|
case 'p': |
|
bufSize = IMGBUF_SIZE2; |
|
break; |
|
case 'R': |
|
R = 1; |
|
break; |
case 'v': |
case 'v': |
Verbose++; |
Verbose++; |
break; |
break; |
Line 104 main(int argc, char **argv)
|
Line 173 main(int argc, char **argv)
|
} |
} |
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
if (!argc || !*imgName || !imgSize) { |
|
Usage(); |
|
return 1; |
|
} else |
|
strlcpy(imgFile, *argv, sizeof imgFile); |
|
|
|
VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", imgSize, imgName, imgFile); | 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 |
| 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", |
| iSize, imgSize); |
| return 2; |
| } else |
| lseek(fd, 0, SEEK_SET); |
| } else if (!imgSize) { |
| Usage(); |
| return 1; |
| } else |
| fd = STDIN_FILENO; |
| } 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 if (!imgSize) { |
| Usage(); |
| return 1; |
| } else |
| fd = STDOUT_FILENO; |
| } |
|
|
/* open image file */ | VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", |
fd = open(imgFile, O_RDONLY); | imgSize, imgName, argc ? imgFile : "<stdin>"); |
if (fd == -1) { | |
ESYSERR(0); | |
return 2; | |
} else | |
fSize = lseek(fd, 0, SEEK_END); | |
if (fSize == -1 || fSize > imgSize) { | |
close(fd); | |
printf("Error:: file size %llu is greater from storage size %llu\n", | |
fSize, imgSize); | |
return 2; | |
} else | |
lseek(fd, 0, SEEK_SET); | |
|
|
/* open storage device */ | if (!m) { |
img = open(imgName, O_RDWR | O_CREAT, 0644); | /* open storage device */ |
if (img == -1) { | img = open(imgName, O_RDWR | O_CREAT | tr, 0644); |
ESYSERR(0); | if (img == -1) { |
close(fd); | ESYSERR(0); |
return 3; | if (fd > 2) |
| close(fd); |
| return 3; |
| } |
| } else { /* GET */ |
| /* open storage device */ |
| img = open(imgName, O_RDONLY); |
| if (img == -1) { |
| ESYSERR(0); |
| if (fd > 2) |
| close(fd); |
| return 3; |
| } else |
| 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", |
| iSize, imgSize); |
| return 3; |
| } else |
| lseek(img, 0, SEEK_SET); |
} |
} |
if (EmptyStore(img) == -1 || iSize < imgSize) { |
|
close(fd); |
|
close(img); |
|
unlink(imgName); |
|
printf("Error:: current storage size %llu is smaller from storage size %llu\n", |
|
iSize, imgSize); |
|
return 3; |
|
} else |
|
lseek(img, 0, SEEK_SET); |
|
|
|
if (FillStore(img, fd) == -1) { | if (!m) { |
close(fd); | if (EmptyStore(img) == -1) { |
close(img); | if (fd > 2) |
unlink(imgName); | close(fd); |
return 4; | close(img); |
| return 3; |
| } |
| if (FillStore(img, fd) == -1) { |
| if (fd > 2) |
| close(fd); |
| close(img); |
| return 4; |
| } |
| } else { /* GET */ |
| if (EmptyStore(fd) == -1) { |
| if (fd > 2) |
| close(fd); |
| close(img); |
| return 3; |
| } |
| if (FillStore(fd, img) == -1) { |
| if (fd > 2) |
| close(fd); |
| close(img); |
| return 4; |
| } |
} |
} |
|
|
close(img); |
close(img); |
close(fd); | if (fd > 2) |
| close(fd); |
| |
| if (R) |
| reboot(RB_AUTOBOOT); |
return 0; |
return 0; |
} |
} |