|
|
| version 1.1.2.1, 2014/02/05 12:37:31 | version 1.1.2.3, 2014/02/05 13:57:01 |
|---|---|
| Line 14 Usage() | Line 14 Usage() |
| "=== %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-t\t\tTruncate Storage file name\n" |
| "\t-s <size>\tStorage image file to size\n" | |
| "\t-f <devfile>\tStorage file name\n" | "\t-f <devfile>\tStorage file name\n" |
| "\n", compiled, compiledby, compilehost); | "\n", compiled, compiledby, compilehost); |
| } | } |
| Line 26 EmptyStore(int img) | Line 27 EmptyStore(int img) |
| u_char buf[IMGBLOCK_SIZE]; | u_char buf[IMGBLOCK_SIZE]; |
| ssize_t wlen; | ssize_t wlen; |
| 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, IMGBLOCK_SIZE); | |
| memset(buf, 0, sizeof buf); | memset(buf, 0, sizeof buf); |
| for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++) | for (i = howmany(iSize, IMGBLOCK_SIZE); i < howmany(imgSize, IMGBLOCK_SIZE); i++) |
| if ((wlen = write(img, buf, sizeof buf)) == -1 || | if ((wlen = write(img, buf, sizeof buf)) == -1 || |
| wlen != sizeof buf) { | wlen != sizeof buf) { |
| printf("Error:: at chunk %d init %d bytes, should be %u", | EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", |
| i, wlen, sizeof buf); | i, wlen, sizeof buf); |
| 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 | |
| FillStore(int img, int fd) | |
| { | |
| register int i, j; | |
| u_char buf[IMGBLOCK_SIZE]; | |
| ssize_t rlen, wlen; | |
| VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile); | |
| iSize = lseek(img, 0, SEEK_CUR); | |
| printf("n_%lld\n", iSize); | |
| for (j = 0, i = howmany(iSize, IMGBLOCK_SIZE); i < howmany(imgSize, IMGBLOCK_SIZE); | |
| i++, j++) { | |
| memset(buf, 0, sizeof buf); | |
| rlen = read(fd, buf, sizeof buf); | |
| if (rlen == -1) { | |
| ESYSERR(0); | |
| return -1; | |
| } else if (!rlen) | |
| break; | |
| else | |
| VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j); | |
| wlen = write(img, buf, rlen); | |
| if (wlen == -1) { | |
| ESYSERR(0); | |
| return -1; | |
| } else if (!wlen || wlen != rlen) { | |
| EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", | |
| rlen, wlen); | |
| } else | |
| VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i); | |
| } | |
| return 0; | |
| } | |
| int | int |
| main(int argc, char **argv) | main(int argc, char **argv) |
| { | { |
| char ch; | char ch; |
| int fd, img; | int fd, img, tr = 0; |
| while ((ch = getopt(argc, argv, "hvs:f:")) != -1) | while ((ch = getopt(argc, argv, "hvts:f:")) != -1) |
| switch (ch) { | switch (ch) { |
| case 'f': | case 'f': |
| strlcpy(imgName, optarg, sizeof imgName); | strlcpy(imgName, optarg, sizeof imgName); |
| Line 58 main(int argc, char **argv) | Line 105 main(int argc, char **argv) |
| return 1; | return 1; |
| } | } |
| break; | break; |
| case 't': | |
| tr = O_TRUNC; | |
| break; | |
| case 'v': | case 'v': |
| Verbose++; | Verbose++; |
| break; | break; |
| Line 85 main(int argc, char **argv) | Line 135 main(int argc, char **argv) |
| fSize = lseek(fd, 0, SEEK_END); | fSize = lseek(fd, 0, SEEK_END); |
| if (fSize == -1 || fSize > imgSize) { | if (fSize == -1 || fSize > imgSize) { |
| close(fd); | close(fd); |
| printf("Error:: file size %llu is greater from storage size %llu\n", | EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n", |
| fSize, imgSize); | fSize, imgSize); |
| return 2; | return 2; |
| } else | } else |
| lseek(fd, 0, SEEK_SET); | lseek(fd, 0, SEEK_SET); |
| /* open storage device */ | /* open storage device */ |
| img = open(imgName, O_RDWR | O_CREAT, 0644); | img = open(imgName, O_RDWR | O_CREAT | tr, 0644); |
| if (img == -1) { | if (img == -1) { |
| ESYSERR(0); | ESYSERR(0); |
| close(fd); | close(fd); |
| return 3; | return 3; |
| } | } |
| if (EmptyStore(img) == -1 || iSize < imgSize) { | if (EmptyStore(img) == -1) { |
| close(fd); | close(fd); |
| close(img); | close(img); |
| unlink(imgName); | unlink(imgName); |
| printf("Error:: current storage size %llu is smaller from storage size %llu\n", | |
| iSize, imgSize); | |
| return 3; | return 3; |
| } else | } |
| lseek(img, 0, SEEK_SET); | |
| if (FillStore(img, fd) == -1) { | |
| close(fd); | |
| close(img); | |
| unlink(imgName); | |
| return 4; | |
| } | |
| close(img); | close(img); |
| close(fd); | close(fd); |