Annotation of fwmaker/src/cmds.c, revision 1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>