Annotation of embedtools/src/imgupd.c, revision 1.1.2.1
1.1.2.1 ! misho 1: #include "global.h"
! 2:
! 3:
! 4: char imgName[PATH_MAX], imgFile[PATH_MAX];
! 5: off_t imgSize, fSize, iSize;
! 6: int Verbose;
! 7: extern char compiled[], compiledby[], compilehost[];
! 8:
! 9: static void
! 10: Usage()
! 11: {
! 12:
! 13: printf( "IMGUPD is tool for management of images\n"
! 14: "=== %s === %s@%s ===\n\n"
! 15: " Syntax: imgupd [options] <image_file>\n\n"
! 16: "\t-v\t\tVerbose ...\n"
! 17: "\t-s <size>\tStorage size\n"
! 18: "\t-f <devfile>\tStorage file name\n"
! 19: "\n", compiled, compiledby, compilehost);
! 20: }
! 21:
! 22: static int
! 23: EmptyStore(int img)
! 24: {
! 25: register int i;
! 26: u_char buf[IMGBLOCK_SIZE];
! 27: ssize_t wlen;
! 28:
! 29: memset(buf, 0, sizeof buf);
! 30: for (i = 0; i < howmany(imgSize, IMGBLOCK_SIZE); i++)
! 31: if ((wlen = write(img, buf, sizeof buf)) == -1 ||
! 32: wlen != sizeof buf) {
! 33: printf("Error:: at chunk %d init %d bytes, should be %u",
! 34: i, wlen, sizeof buf);
! 35: return -1;
! 36: } else
! 37: VERB(1) printf("+Written chunk #%d\n", i);
! 38:
! 39: iSize = lseek(img, 0, SEEK_END);
! 40: return iSize;
! 41: }
! 42:
! 43: int
! 44: main(int argc, char **argv)
! 45: {
! 46: char ch;
! 47: int fd, img;
! 48:
! 49: while ((ch = getopt(argc, argv, "hvs:f:")) != -1)
! 50: switch (ch) {
! 51: case 'f':
! 52: strlcpy(imgName, optarg, sizeof imgName);
! 53: break;
! 54: case 's':
! 55: imgSize = strtoll(optarg, NULL, 0);
! 56: if (!imgSize) {
! 57: Usage();
! 58: return 1;
! 59: }
! 60: break;
! 61: case 'v':
! 62: Verbose++;
! 63: break;
! 64: case 'h':
! 65: default:
! 66: Usage();
! 67: return 1;
! 68: }
! 69: argc -= optind;
! 70: argv += optind;
! 71: if (!argc || !*imgName || !imgSize) {
! 72: Usage();
! 73: return 1;
! 74: } else
! 75: strlcpy(imgFile, *argv, sizeof imgFile);
! 76:
! 77: VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", imgSize, imgName, imgFile);
! 78:
! 79: /* open image file */
! 80: fd = open(imgFile, O_RDONLY);
! 81: if (fd == -1) {
! 82: ESYSERR(0);
! 83: return 2;
! 84: } else
! 85: fSize = lseek(fd, 0, SEEK_END);
! 86: if (fSize == -1 || fSize > imgSize) {
! 87: close(fd);
! 88: printf("Error:: file size %llu is greater from storage size %llu\n",
! 89: fSize, imgSize);
! 90: return 2;
! 91: } else
! 92: lseek(fd, 0, SEEK_SET);
! 93:
! 94: /* open storage device */
! 95: img = open(imgName, O_RDWR | O_CREAT, 0644);
! 96: if (img == -1) {
! 97: ESYSERR(0);
! 98: close(fd);
! 99: return 3;
! 100: }
! 101: if (EmptyStore(img) == -1 || iSize < imgSize) {
! 102: close(fd);
! 103: close(img);
! 104: unlink(imgName);
! 105: printf("Error:: current storage size %llu is smaller from storage size %llu\n",
! 106: iSize, imgSize);
! 107: return 3;
! 108: } else
! 109: lseek(img, 0, SEEK_SET);
! 110:
! 111: close(img);
! 112: close(fd);
! 113: return 0;
! 114: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>