--- embedtools/src/imgupd.c 2014/02/05 12:59:55 1.1.2.2 +++ embedtools/src/imgupd.c 2014/02/05 13:57:01 1.1.2.3 @@ -14,7 +14,8 @@ Usage() "=== %s === %s@%s ===\n\n" " Syntax: imgupd [options] \n\n" "\t-v\t\tVerbose ...\n" - "\t-s \tStorage size\n" + "\t-t\t\tTruncate Storage file name\n" + "\t-s \tStorage image file to size\n" "\t-f \tStorage file name\n" "\n", compiled, compiledby, compilehost); } @@ -28,8 +29,15 @@ EmptyStore(int img) 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); - 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 || wlen != sizeof buf) { EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", @@ -38,20 +46,23 @@ EmptyStore(int img) } else VERB(1) printf("+Written chunk #%d\n", i); - iSize = lseek(img, 0, SEEK_END); + iSize = lseek(img, iSize, SEEK_SET); return iSize; } static int FillStore(int img, int fd) { - register int i; + 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); - for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++) { + 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) { @@ -60,7 +71,7 @@ FillStore(int img, int fd) } else if (!rlen) break; 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); if (wlen == -1) { @@ -80,9 +91,9 @@ int main(int argc, char **argv) { 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) { case 'f': strlcpy(imgName, optarg, sizeof imgName); @@ -94,6 +105,9 @@ main(int argc, char **argv) return 1; } break; + case 't': + tr = O_TRUNC; + break; case 'v': Verbose++; break; @@ -121,28 +135,25 @@ main(int argc, char **argv) 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", + EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n", fSize, imgSize); return 2; } else lseek(fd, 0, SEEK_SET); /* open storage device */ - img = open(imgName, O_RDWR | O_CREAT, 0644); + img = open(imgName, O_RDWR | O_CREAT | tr, 0644); if (img == -1) { ESYSERR(0); close(fd); return 3; } - if (EmptyStore(img) == -1 || iSize < imgSize) { + if (EmptyStore(img) == -1) { 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) { close(fd);