Annotation of embedtools/src/imgupd.c, revision 1.1.2.3
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"
1.1.2.3 ! misho 17: "\t-t\t\tTruncate Storage file name\n"
! 18: "\t-s <size>\tStorage image file to size\n"
1.1.2.1 misho 19: "\t-f <devfile>\tStorage file name\n"
20: "\n", compiled, compiledby, compilehost);
21: }
22:
23: static int
24: EmptyStore(int img)
25: {
26: register int i;
27: u_char buf[IMGBLOCK_SIZE];
28: ssize_t wlen;
29:
1.1.2.2 misho 30: VERB(1) printf("Erase store %s\n", imgName);
31:
1.1.2.3 ! misho 32: iSize = lseek(img, 0, SEEK_END);
! 33: if (iSize == -1) {
! 34: ESYSERR(0);
! 35: return -1;
! 36: } else
! 37: imgSize += E_ALIGN(iSize, IMGBLOCK_SIZE);
! 38:
1.1.2.1 misho 39: memset(buf, 0, sizeof buf);
1.1.2.3 ! misho 40: for (i = howmany(iSize, IMGBLOCK_SIZE); i < howmany(imgSize, IMGBLOCK_SIZE); i++)
1.1.2.1 misho 41: if ((wlen = write(img, buf, sizeof buf)) == -1 ||
42: wlen != sizeof buf) {
1.1.2.2 misho 43: EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n",
1.1.2.1 misho 44: i, wlen, sizeof buf);
45: return -1;
46: } else
47: VERB(1) printf("+Written chunk #%d\n", i);
48:
1.1.2.3 ! misho 49: iSize = lseek(img, iSize, SEEK_SET);
1.1.2.1 misho 50: return iSize;
51: }
52:
1.1.2.2 misho 53: static int
54: FillStore(int img, int fd)
55: {
1.1.2.3 ! misho 56: register int i, j;
1.1.2.2 misho 57: u_char buf[IMGBLOCK_SIZE];
58: ssize_t rlen, wlen;
59:
60: VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
61:
1.1.2.3 ! misho 62: iSize = lseek(img, 0, SEEK_CUR);
! 63: printf("n_%lld\n", iSize);
! 64: for (j = 0, i = howmany(iSize, IMGBLOCK_SIZE); i < howmany(imgSize, IMGBLOCK_SIZE);
! 65: i++, j++) {
1.1.2.2 misho 66: memset(buf, 0, sizeof buf);
67: rlen = read(fd, buf, sizeof buf);
68: if (rlen == -1) {
69: ESYSERR(0);
70: return -1;
71: } else if (!rlen)
72: break;
73: else
1.1.2.3 ! misho 74: VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
1.1.2.2 misho 75:
76: wlen = write(img, buf, rlen);
77: if (wlen == -1) {
78: ESYSERR(0);
79: return -1;
80: } else if (!wlen || wlen != rlen) {
81: EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n",
82: rlen, wlen);
83: } else
84: VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
85: }
86:
87: return 0;
88: }
89:
1.1.2.1 misho 90: int
91: main(int argc, char **argv)
92: {
93: char ch;
1.1.2.3 ! misho 94: int fd, img, tr = 0;
1.1.2.1 misho 95:
1.1.2.3 ! misho 96: while ((ch = getopt(argc, argv, "hvts:f:")) != -1)
1.1.2.1 misho 97: switch (ch) {
98: case 'f':
99: strlcpy(imgName, optarg, sizeof imgName);
100: break;
101: case 's':
102: imgSize = strtoll(optarg, NULL, 0);
103: if (!imgSize) {
104: Usage();
105: return 1;
106: }
107: break;
1.1.2.3 ! misho 108: case 't':
! 109: tr = O_TRUNC;
! 110: break;
1.1.2.1 misho 111: case 'v':
112: Verbose++;
113: break;
114: case 'h':
115: default:
116: Usage();
117: return 1;
118: }
119: argc -= optind;
120: argv += optind;
121: if (!argc || !*imgName || !imgSize) {
122: Usage();
123: return 1;
124: } else
125: strlcpy(imgFile, *argv, sizeof imgFile);
126:
127: VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", imgSize, imgName, imgFile);
128:
129: /* open image file */
130: fd = open(imgFile, O_RDONLY);
131: if (fd == -1) {
132: ESYSERR(0);
133: return 2;
134: } else
135: fSize = lseek(fd, 0, SEEK_END);
136: if (fSize == -1 || fSize > imgSize) {
137: close(fd);
1.1.2.3 ! misho 138: EERROR(ENOSPC, "Error:: file size %llu is greater from storage size %llu\n",
1.1.2.1 misho 139: fSize, imgSize);
140: return 2;
141: } else
142: lseek(fd, 0, SEEK_SET);
143:
144: /* open storage device */
1.1.2.3 ! misho 145: img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
1.1.2.1 misho 146: if (img == -1) {
147: ESYSERR(0);
148: close(fd);
149: return 3;
150: }
1.1.2.3 ! misho 151: if (EmptyStore(img) == -1) {
1.1.2.1 misho 152: close(fd);
153: close(img);
154: unlink(imgName);
155: return 3;
1.1.2.3 ! misho 156: }
1.1.2.1 misho 157:
1.1.2.2 misho 158: if (FillStore(img, fd) == -1) {
159: close(fd);
160: close(img);
161: unlink(imgName);
162: return 4;
163: }
164:
1.1.2.1 misho 165: close(img);
166: close(fd);
167: return 0;
168: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>