|
version 1.2, 2014/02/05 15:44:06
|
version 1.6.2.5, 2014/02/06 09:19:51
|
|
Line 47 SUCH DAMAGE.
|
Line 47 SUCH DAMAGE.
|
| |
|
| |
|
| char imgName[PATH_MAX], imgFile[PATH_MAX]; |
char imgName[PATH_MAX], imgFile[PATH_MAX]; |
| off_t imgSize, iSize; | off_t imgSize, iSize, fromBegin; |
| int Verbose; | int Verbose, bufSize = IMGBUF_SIZE; |
| extern char compiled[], compiledby[], compilehost[]; |
extern char compiled[], compiledby[], compilehost[]; |
| |
|
| static void |
static void |
|
Line 57 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-R\t\tReboot system after complete\n" |
| |
"\t-p\t\tPipe suitable transfer on little chunks\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-t\t\tTruncate Storage file name\n" |
| "\t-s <size>\tStorage size (required for stdin)\n" |
"\t-s <size>\tStorage size (required for stdin)\n" |
| "\t-f <devfile>\tStorage file name\n" |
"\t-f <devfile>\tStorage file name\n" |
|
Line 69 static int
|
Line 73 static int
|
| EmptyStore(int img) |
EmptyStore(int img) |
| { |
{ |
| register int i; |
register int i; |
| u_char buf[IMGBUF_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 (!fromBegin) { |
| if (iSize == -1) { | iSize = lseek(img, 0, SEEK_END); |
| ESYSERR(0); | if (iSize == -1) { |
| return -1; | ESYSERR(0); |
| | return -1; |
| | } else |
| | imgSize += E_ALIGN(iSize, bufSize); |
| } else |
} else |
| imgSize += E_ALIGN(iSize, IMGBUF_SIZE); | iSize ^= iSize; |
| |
|
| memset(buf, 0, sizeof buf); |
memset(buf, 0, sizeof buf); |
| for (i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_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", | if (wlen == -1) |
| i, wlen, sizeof buf); | ESYSERR(0); |
| | else |
| | EERROR(EIO, "Error at chunk %d init %d bytes, " |
| | "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); |
|
Line 99 static int
|
Line 109 static int
|
| FillStore(int img, int fd) |
FillStore(int img, int fd) |
| { |
{ |
| register int i, j; |
register int i, j; |
| u_char buf[IMGBUF_SIZE]; | u_char *pos, buf[bufSize]; |
| ssize_t rlen, wlen; | ssize_t rlen, wlen, len; |
| |
|
| 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 (j = 0, i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); | for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); |
| i++, j++) { |
i++, j++) { |
| memset(buf, 0, sizeof buf); |
memset(buf, 0, sizeof buf); |
| rlen = read(fd, buf, sizeof buf); | for (len = bufSize, pos = buf; len > 0; len -= rlen, pos += rlen) { |
| if (rlen == -1) { | rlen = read(fd, pos, len); |
| ESYSERR(0); | if (rlen == -1) { |
| return -1; | ESYSERR(0); |
| } else if (!rlen) | return -1; |
| break; | } else if (!rlen) |
| else | break; |
| VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j); | else |
| | VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j); |
| | } |
| |
|
| wlen = write(img, buf, rlen); | wlen = write(img, buf, bufSize); |
| if (wlen == -1) { |
if (wlen == -1) { |
| ESYSERR(0); |
ESYSERR(0); |
| return -1; |
return -1; |
| } else if (!wlen || wlen != rlen) { | } else if (wlen && wlen != bufSize) { |
| EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", | EERROR(EIO, "Error at chunk %d written %d bytes, " |
| rlen, wlen); | "should be %u\n", i, wlen, bufSize); |
| } else |
} else |
| VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i); |
VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i); |
| } |
} |
|
Line 133 FillStore(int img, int fd)
|
Line 145 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, tr = 0; |
int fd, img, tr = 0; |
| |
|
| while ((ch = getopt(argc, argv, "hvts:f:")) != -1) | while ((ch = getopt(argc, argv, "hvRipgts:f:")) != -1) |
| switch (ch) { |
switch (ch) { |
| case 'f': |
case 'f': |
| strlcpy(imgName, optarg, sizeof imgName); |
strlcpy(imgName, optarg, sizeof imgName); |
|
Line 151 main(int argc, char **argv)
|
Line 163 main(int argc, char **argv)
|
| case 't': |
case 't': |
| tr = O_TRUNC; |
tr = O_TRUNC; |
| break; |
break; |
| |
case 'p': |
| |
bufSize = IMGBUF_SIZE2; |
| |
break; |
| |
case 'g': |
| |
m = 1; |
| |
case 'i': |
| |
fromBegin = 1; |
| |
break; |
| |
case 'R': |
| |
R = 1; |
| |
break; |
| case 'v': |
case 'v': |
| Verbose++; |
Verbose++; |
| break; |
break; |
|
Line 162 main(int argc, char **argv)
|
Line 185 main(int argc, char **argv)
|
| argc -= optind; |
argc -= optind; |
| argv += optind; |
argv += optind; |
| |
|
| if (argc) { | if (!m) { |
| strlcpy(imgFile, *argv, sizeof imgFile); | if (argc) { |
| /* open image file */ | strlcpy(imgFile, *argv, sizeof imgFile); |
| fd = open(imgFile, O_RDONLY); | /* open image file */ |
| if (fd == -1) { | fd = open(imgFile, O_RDONLY); |
| ESYSERR(0); | if (fd == -1) { |
| return 2; | 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 |
} else |
| iSize = lseek(fd, 0, SEEK_END); | fd = STDIN_FILENO; |
| if (!imgSize) | } else { /* GET */ |
| imgSize = E_ALIGN(iSize, IMGBUF_SIZE); | if (argc) { |
| if (iSize == -1 || iSize > imgSize) { | strlcpy(imgFile, *argv, sizeof imgFile); |
| close(fd); | /* open image file */ |
| EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n", | fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644); |
| iSize, imgSize); | if (fd == -1) { |
| return 2; | ESYSERR(0); |
| | return 2; |
| | } |
| | } else if (!imgSize) { |
| | Usage(); |
| | return 1; |
| } else |
} else |
| lseek(fd, 0, SEEK_SET); | fd = STDOUT_FILENO; |
| } else if (!imgSize) { | } |
| Usage(); | |
| return 1; | |
| } else | |
| fd = STDIN_FILENO; | |
| |
|
| VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", |
VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", |
| imgSize, imgName, argc ? imgFile : "<stdin>"); |
imgSize, imgName, argc ? imgFile : "<stdin>"); |
| |
|
| /* open storage device */ | if (!m) { |
| img = open(imgName, O_RDWR | O_CREAT | tr, 0644); | /* open storage device */ |
| if (img == -1) { | img = open(imgName, O_RDWR | O_CREAT | tr, 0644); |
| ESYSERR(0); | if (img == -1) { |
| if (fd > 2) | ESYSERR(0); |
| close(fd); | if (fd > 2) |
| return 3; | 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) { |
|
| if (fd > 2) |
|
| close(fd); |
|
| close(img); |
|
| unlink(imgName); |
|
| return 3; |
|
| } |
|
| |
|
| if (FillStore(img, fd) == -1) { | if (!m) { |
| if (fd > 2) | if (EmptyStore(img) == -1) { |
| close(fd); | if (fd > 2) |
| close(img); | close(fd); |
| unlink(imgName); | close(img); |
| return 4; | return 3; |
| | } |
| | if (FillStore(img, fd) == -1) { |
| | if (fd > 2) |
| | close(fd); |
| | close(img); |
| | return 4; |
| | } |
| | } else { /* GET */ |
| | iSize ^= iSize; |
| | 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); |
| if (fd > 2) |
if (fd > 2) |
| close(fd); |
close(fd); |
| |
|
| |
if (R) |
| |
reboot(RB_AUTOBOOT); |
| return 0; |
return 0; |
| } |
} |