|
|
1.1 misho 1: #include "global.h"
2: #include "cmds.h"
3: #include "image.h"
4:
5:
6: int
7: cmd_Show(void *lb, int idx, char **args)
8: {
9: ETRACE();
10:
11: printf("FW Image file: %s\n", image);
12: printf("FW Image size: %zu\n", imgsiz);
13: printf("FW Image descriptor: %d\n\n", imgfd);
14:
15: printf("MTD loaded: 0x%zx\n", mtd.mtd_fsiz);
16: printf("MTD file: %s\n", mtd.mtd_file);
17: printf("MTD offset: 0x%lx\n", mtd.mtd_offset);
18: printf("MTD size: 0x%zx\n", mtd.mtd_size);
19:
20: return 0;
21: }
22:
23: static int
24: dialog(void *lb, const char *prompt, char *str, int len)
25: {
26: int ret = 0;
27: char *p;
28:
29: if (!str || len < 1)
30: return -1;
31:
32: cli_Printf(lb, "%s", prompt);
33: p = cliInputLine(lb, -1);
34: if (p) {
35: cli_freeInput(lb);
36: strlcpy(str, p, len);
37: e_free(p);
38:
39: if ((p = strpbrk(str, "\r\n")))
40: *p = 0;
41: }
42:
43: return ret;
44: }
45:
46: static void
47: loadparams(void *lb, char ** const args)
48: {
49: char str[STRSIZ];
50:
51: memset(&mtd, 0, sizeof mtd);
52: if (!args[1]) {
53: if (!dialog(lb, "Offset=", str, sizeof str))
54: mtd.mtd_offset = (off_t) strtol(str, NULL, 0);
55: } else
56: mtd.mtd_offset = (off_t) strtol(args[1], NULL, 0);
57: EVERBOSE(3, "mtd.offset=0x%lx", mtd.mtd_offset);
58: if (!args[2]) {
59: if (!dialog(lb, "Size=", str, sizeof str))
60: mtd.mtd_size = (size_t) strtol(str, NULL, 0);
61: } else
62: mtd.mtd_size = (size_t) strtol(args[2], NULL, 0);
63: EVERBOSE(3, "mtd.size=0x%zx", mtd.mtd_size);
64: }
65:
66: static int
67: openfile(int flgs)
68: {
69: int fd;
70:
71: fd = open(mtd.mtd_file, flgs, 0644);
72: if (fd == -1) {
73: ESYSERR(0);
74: return -1;
75: }
76: if (lseek(imgfd, mtd.mtd_offset, SEEK_SET) == -1) {
77: ESYSERR(0);
78: close(fd);
79: return -1;
80: }
81:
82: EVERBOSE(1, "mtd_file %s opened #%d", mtd.mtd_file, fd);
83: return fd;
84: }
85:
86: static void
87: closefile(int fd)
88: {
89: EVERBOSE(1, "mtd_file closed #%d", fd);
90: close(fd);
91: }
92:
93: int
94: cmd_Mtd(void *lb, int idx, char **args)
95: {
96: char str[MAXPATHLEN];
97: struct stat st;
98: int fd, wlen = 0, rlen = 0;
99: u_char buf[BUFSIZ] = { 0 };
100:
101: ETRACE();
102:
103: args++;
104: if (!args[0]) {
105: printf("Help: mtd [read|write|erase] <mtd_offset> <mtd_size> [mtd_bin_file]\n");
106: return -1;
107: } else if (!strncmp(args[0], "write", strlen(args[0]))) {
108: loadparams(lb, args);
109: if (!args[3]) {
110: if (!dialog(lb, "File=", str, sizeof str))
111: strlcpy(mtd.mtd_file, str, sizeof mtd.mtd_file);
112: } else
113: strlcpy(mtd.mtd_file, args[3], sizeof mtd.mtd_file);
114: EVERBOSE(3, "mtd.file=%s", mtd.mtd_file);
115:
116: if (stat(mtd.mtd_file, &st) == -1) {
117: ESYSERR(0);
118: memset(&mtd, 0, sizeof mtd);
119: return -1;
120: } else
121: mtd.mtd_fsiz = st.st_size;
122: EVERBOSE(3, "mtd.fsiz=%lu", mtd.mtd_fsiz);
123: if (mtd.mtd_size < mtd.mtd_fsiz) {
124: printf("Error:: MTD file is bigger then reserved space!\n");
125: memset(&mtd, 0, sizeof mtd);
126: return -1;
127: }
128: if (imgfd < 3) {
129: printf("Error:: image isn't present!\n");
130: memset(&mtd, 0, sizeof mtd);
131: return -1;
132: }
133: if (imgsiz < mtd.mtd_offset + mtd.mtd_size) {
134: printf("Error:: requested region for write is out of image size\n");
135: memset(&mtd, 0, sizeof mtd);
136: return -1;
137: }
138:
139: fd = openfile(O_RDONLY);
140: if (fd == -1) {
141: memset(&mtd, 0, sizeof mtd);
142: return -1;
143: }
144: while ((rlen = read(fd, buf, sizeof buf)) > 0)
145: if ((rlen = write(imgfd, buf, rlen)) == -1)
146: break;
147: else
148: wlen += rlen;
149: if (rlen == -1) {
150: ESYSERR(0);
151: memset(&mtd, 0, sizeof mtd);
152: } else if (!rlen)
153: printf("MTD partition size=%d was written to image\n", wlen);
154: closefile(fd);
155: } else if (!strncmp(args[0], "read", strlen(args[0]))) {
156: loadparams(lb, args);
157: if (!args[3]) {
158: if (!dialog(lb, "File=", str, sizeof str))
159: strlcpy(mtd.mtd_file, str, sizeof mtd.mtd_file);
160: } else
161: strlcpy(mtd.mtd_file, args[3], sizeof mtd.mtd_file);
162: EVERBOSE(3, "mtd.file=%s", mtd.mtd_file);
163:
164: if (imgfd < 3) {
165: printf("Error:: image isn't present!\n");
166: memset(&mtd, 0, sizeof mtd);
167: return -1;
168: }
169: if (imgsiz < mtd.mtd_offset + mtd.mtd_size) {
170: printf("Error:: requested region for read is out of image size\n");
171: memset(&mtd, 0, sizeof mtd);
172: return -1;
173: }
174:
175: fd = openfile(O_WRONLY | O_CREAT);
176: if (fd == -1) {
177: memset(&mtd, 0, sizeof mtd);
178: return -1;
179: }
180: while (wlen < mtd.mtd_size &&
181: (rlen = read(imgfd, buf, MIN(mtd.mtd_size - wlen, sizeof buf))) > 0)
182: if ((rlen = write(fd, buf, rlen)) == -1)
183: break;
184: else
185: wlen += rlen;
186: if (rlen == -1) {
187: ESYSERR(0);
188: memset(&mtd, 0, sizeof mtd);
189: } else
190: printf("MTD image partition size=%d was transferred file\n", wlen);
191: closefile(fd);
192: } else if (!strncmp(args[0], "erase", strlen(args[0]))) {
193: loadparams(lb, args);
194:
195: if (imgfd < 3) {
196: printf("Error:: image isn't present!\n");
197: memset(&mtd, 0, sizeof mtd);
198: return -1;
199: }
200: if (imgsiz < mtd.mtd_offset + mtd.mtd_size) {
201: printf("Error:: requested region for erase is out of image size\n");
202: memset(&mtd, 0, sizeof mtd);
203: return -1;
204: }
205: if (lseek(imgfd, mtd.mtd_offset, SEEK_SET) == -1) {
206: ESYSERR(0);
207: memset(&mtd, 0, sizeof mtd);
208: return -1;
209: }
210: while (wlen < mtd.mtd_size)
211: if ((rlen = write(imgfd, buf, MIN(mtd.mtd_size - wlen, sizeof buf))) == -1)
212: break;
213: else
214: wlen += rlen;
215: if (rlen == -1) {
216: ESYSERR(0);
217: memset(&mtd, 0, sizeof mtd);
218: } else
219: printf("MTD image partition size=%d was erased\n", wlen);
220: } else {
221: printf("Error:: unknown command '%s'\n", args[0]);
222: return -1;
223: }
224:
225: return 0;
226: }
227:
228: int
229: cmd_Image(void *lb, int idx, char **args)
230: {
231: ETRACE();
232:
233: args++;
234: if (!args[0]) {
235: printf("Help: image [mount|umount|erase|name <file>|init <size>]\n");
236: return -1;
237: } else if (!strncmp(args[0], "mount", strlen(args[0]))) {
238: if (imgfd > 2) {
239: printf("Error:: Image is already mounted ...\n");
240: return -1;
241: }
242: imageOpen(0);
243: } else if (!strncmp(args[0], "umount", strlen(args[0]))) {
244: imageClose();
245: } else if (!strcmp(args[0], "erase")) {
246: if (imgfd > 2) {
247: printf("Error:: Image is mounted, cannot be erased ...\n");
248: return -1;
249: }
250: unlink(image);
251: EVERBOSE(1, "Erase image %s\n", image);
252: } else if (!strncmp(args[0], "name", strlen(args[0]))) {
253: if (imgfd > 2) {
254: printf("Error:: Image is mounted, cannot be renamed ...\n");
255: return -1;
256: }
257: if (!args[1]) {
258: printf("Error:: name isn't found!\n");
259: return -1;
260: }
261: strlcpy(image, args[1], sizeof image);
262: } else if (!strncmp(args[0], "init", strlen(args[0]))) {
263: if (imgfd > 2) {
264: printf("Error:: Image is already mounted, cannot be inited ...\n");
265: return -1;
266: }
267: if (!args[1]) {
268: printf("Error:: size isn't found!\n");
269: return -1;
270: }
271: if (imageOpen(O_CREAT) > 2) {
272: imgsiz = (size_t) strtol(args[1], NULL, 0);
273: if (ftruncate(imgfd, imgsiz) == -1) {
274: ESYSERR(0);
275: imageClose();
276: unlink(image);
277: return -1;
278: } else
279: EVERBOSE(1, "Image '%s' created with size=%lu\n", image, imgsiz);
280: }
281: } else {
282: printf("Error:: unknown command '%s'\n", args[0]);
283: return -1;
284: }
285:
286: return 0;
287: }