Annotation of embedtools/src/imgupd.c, revision 1.6.2.6

1.2       misho       1: /*************************************************************************
                      2:  * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.6.2.6 ! misho       6:  * $Id: imgupd.c,v 1.6.2.5 2014/02/06 09:19:51 misho Exp $
1.2       misho       7:  *
                      8:  *************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004 - 2014
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
1.6.2.6 ! misho      47: #include "imgupd.h"
1.2       misho      48: 
                     49: 
                     50: char imgName[PATH_MAX], imgFile[PATH_MAX];
1.6.2.1   misho      51: off_t imgSize, iSize, fromBegin;
1.6.2.6 ! misho      52: int Verbose, nameFlg, fileFlg, bufSize = IMGBUF_SIZE;
1.2       misho      53: extern char compiled[], compiledby[], compilehost[];
                     54: 
                     55: static void
                     56: Usage()
                     57: {
                     58: 
                     59:        printf( "IMGUPD is tool for management of images\n"
                     60:                "=== %s === %s@%s ===\n\n"
1.3       misho      61:                "  Syntax: imgupd [options] [image_file]\n\n"
1.2       misho      62:                "\t-v\t\tVerbose ...\n"
1.4       misho      63:                "\t-R\t\tReboot system after complete\n"
                     64:                "\t-p\t\tPipe suitable transfer on little chunks\n"
1.6.2.1   misho      65:                "\t-i\t\tStart fill storage from begin\n"
1.3       misho      66:                "\t-g\t\tGet image from Storage\n"
1.2       misho      67:                "\t-t\t\tTruncate Storage file name\n"
                     68:                "\t-s <size>\tStorage size (required for stdin)\n"
                     69:                "\t-f <devfile>\tStorage file name\n"
                     70:                "\n", compiled, compiledby, compilehost);
                     71: }
                     72: 
                     73: static int
1.6.2.6 ! misho      74: getFDtype(int fd)
        !            75: {
        !            76:        int type, flg = 0;
        !            77:        struct stat sb;
        !            78: 
        !            79:        if (fstat(fd, &sb) == -1) {
        !            80:                ESYSERR(0);
        !            81:                return -1;
        !            82:        }
        !            83:        if (S_ISREG(sb.st_mode))
        !            84:                flg |= FD_TRUNC;
        !            85:        if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
        !            86:                if (ioctl(fd, FIODTYPE, &type) == -1) {
        !            87:                        ESYSERR(0);
        !            88:                        return -1;
        !            89:                }
        !            90:                if (type & D_TAPE)
        !            91:                        flg |= FD_TAPE;
        !            92:                else if (type & (D_DISK | D_MEM))
        !            93:                        flg |= FD_SEEK;
        !            94:                if (S_ISCHR(sb.st_mode) && !(type & D_TAPE))
        !            95:                        flg |= FD_CHR;
        !            96:                goto end;
        !            97:        }
        !            98:        errno ^= errno;
        !            99:        if (lseek(fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
        !           100:                flg |= FD_PIPE;
        !           101:        else
        !           102:                flg |= FD_SEEK;
        !           103: end:
        !           104:        return flg;
        !           105: }
        !           106: 
        !           107: static int
        !           108: EmptyStore(int img, int flg)
1.2       misho     109: {
                    110:        register int i;
1.4       misho     111:        u_char buf[bufSize];
1.2       misho     112:        ssize_t wlen;
                    113: 
                    114:        VERB(1) printf("Erase store %s\n", imgName);
                    115: 
1.6.2.6 ! misho     116:        if (!fromBegin && flg & FD_SEEK) {
1.6.2.1   misho     117:                iSize = lseek(img, 0, SEEK_END);
                    118:                if (iSize == -1) {
                    119:                        ESYSERR(0);
                    120:                        return -1;
                    121:                } else
                    122:                        imgSize += E_ALIGN(iSize, bufSize);
1.2       misho     123:        } else
1.6.2.1   misho     124:                iSize ^= iSize;
1.2       misho     125: 
                    126:        memset(buf, 0, sizeof buf);
1.4       misho     127:        for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++)
1.5       misho     128:                if ((wlen = write(img, buf, bufSize)) == -1 || 
                    129:                                (wlen && wlen != bufSize)) {
1.6.2.4   misho     130:                        if (wlen == -1)
                    131:                                ESYSERR(0);
                    132:                        else
                    133:                                EERROR(EIO, "Error at chunk %d init %d bytes, "
                    134:                                                "should be %u\n", i, wlen, bufSize);
1.2       misho     135:                        return -1;
                    136:                } else
                    137:                        VERB(1) printf("+Written chunk #%d\n", i);
                    138: 
1.6.2.6 ! misho     139:        if (flg & FD_SEEK)
        !           140:                iSize = lseek(img, iSize, SEEK_SET);
1.2       misho     141:        return iSize;
                    142: }
                    143: 
                    144: static int
1.6.2.6 ! misho     145: FillStore(int img, int iflg, int fd, int fflg)
1.2       misho     146: {
                    147:        register int i, j;
1.6.2.5   misho     148:        u_char *pos, buf[bufSize];
                    149:        ssize_t rlen, wlen, len;
1.2       misho     150: 
                    151:        VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
                    152: 
1.4       misho     153:        for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); 
1.2       misho     154:                        i++, j++) {
                    155:                memset(buf, 0, sizeof buf);
1.6.2.5   misho     156:                for (len = bufSize, pos = buf; len > 0; len -= rlen, pos += rlen) {
                    157:                        rlen = read(fd, pos, len);
                    158:                        if (rlen == -1) {
                    159:                                ESYSERR(0);
                    160:                                return -1;
                    161:                        } else if (!rlen)
                    162:                                break;
                    163:                        else
                    164:                                VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
                    165:                }
1.2       misho     166: 
1.6.2.3   misho     167:                wlen = write(img, buf, bufSize);
1.2       misho     168:                if (wlen == -1) {
                    169:                        ESYSERR(0);
                    170:                        return -1;
1.6.2.3   misho     171:                } else if (wlen && wlen != bufSize) {
                    172:                        EERROR(EIO, "Error at chunk %d written %d bytes, "
                    173:                                        "should be %u\n", i, wlen, bufSize);
1.2       misho     174:                } else
                    175:                        VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
                    176:        }
                    177: 
                    178:        return 0;
                    179: }
                    180: 
                    181: int
                    182: main(int argc, char **argv)
                    183: {
1.4       misho     184:        char ch, m = 0, R = 0;
1.2       misho     185:        int fd, img, tr = 0;
                    186: 
1.6.2.1   misho     187:        while ((ch = getopt(argc, argv, "hvRipgts:f:")) != -1)
1.2       misho     188:                switch (ch) {
                    189:                        case 'f':
                    190:                                strlcpy(imgName, optarg, sizeof imgName);
                    191:                                break;
                    192:                        case 's':
                    193:                                imgSize = strtoll(optarg, NULL, 0);
                    194:                                if (!imgSize) {
                    195:                                        Usage();
                    196:                                        return 1;
                    197:                                }
                    198:                                break;
                    199:                        case 't':
                    200:                                tr = O_TRUNC;
                    201:                                break;
1.4       misho     202:                        case 'p':
                    203:                                bufSize = IMGBUF_SIZE2;
                    204:                                break;
1.6.2.2   misho     205:                        case 'g':
                    206:                                m = 1;
1.6.2.1   misho     207:                        case 'i':
                    208:                                fromBegin = 1;
                    209:                                break;
1.4       misho     210:                        case 'R':
                    211:                                R = 1;
                    212:                                break;
1.2       misho     213:                        case 'v':
                    214:                                Verbose++;
                    215:                                break;
                    216:                        case 'h':
                    217:                        default:
                    218:                                Usage();
                    219:                                return 1;
                    220:                }
                    221:        argc -= optind;
                    222:        argv += optind;
                    223: 
1.3       misho     224:        if (!m) {
                    225:                if (argc) {
                    226:                        strlcpy(imgFile, *argv, sizeof imgFile);
                    227:                        /* open image file */
                    228:                        fd = open(imgFile, O_RDONLY);
                    229:                        if (fd == -1) {
                    230:                                ESYSERR(0);
                    231:                                return 2;
                    232:                        } else
1.6.2.6 ! misho     233:                                fileFlg = getFDtype(fd);
        !           234: 
        !           235:                        if (fileFlg & FD_SEEK) {
1.3       misho     236:                                iSize = lseek(fd, 0, SEEK_END);
1.6.2.6 ! misho     237:                                if (!imgSize)
        !           238:                                        imgSize = E_ALIGN(iSize, bufSize);
        !           239:                                if (iSize == -1 || iSize > imgSize) {
        !           240:                                        close(fd);
        !           241:                                        EERROR(ENOSPC, "Error:: file size %llu is "
        !           242:                                                        "greater from storage size %llu\n", 
        !           243:                                                        iSize, imgSize);
        !           244:                                        return 2;
        !           245:                                } else
        !           246:                                        lseek(fd, 0, SEEK_SET);
        !           247:                        }
        !           248:                } else {
        !           249:                        fd = STDIN_FILENO;
        !           250:                        fileFlg = getFDtype(fd);
        !           251:                }
        !           252: 
        !           253:                if (!imgSize) {
        !           254:                        if (fd > 2)
1.3       misho     255:                                close(fd);
                    256:                        Usage();
                    257:                        return 1;
1.6.2.6 ! misho     258:                }
1.3       misho     259:        } else {        /* GET */
                    260:                if (argc) {
                    261:                        strlcpy(imgFile, *argv, sizeof imgFile);
                    262:                        /* open image file */
                    263:                        fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
                    264:                        if (fd == -1) {
                    265:                                ESYSERR(0);
                    266:                                return 2;
1.6.2.6 ! misho     267:                        } else
        !           268:                                fileFlg = getFDtype(fd);
        !           269:                } else {
1.3       misho     270:                        fd = STDOUT_FILENO;
1.6.2.6 ! misho     271:                        fileFlg = getFDtype(fd);
        !           272:                }
1.3       misho     273:        }
                    274: 
                    275:        VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
                    276:                        imgSize, imgName, argc ? imgFile : "<stdin>");
                    277: 
                    278:        if (!m) {
                    279:                /* open storage device */
                    280:                img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
                    281:                if (img == -1) {
1.2       misho     282:                        ESYSERR(0);
1.3       misho     283:                        if (fd > 2)
                    284:                                close(fd);
                    285:                        return 3;
1.6.2.6 ! misho     286:                } else
        !           287:                        nameFlg = getFDtype(img);
1.3       misho     288:        } else {        /* GET */
                    289:                /* open storage device */
                    290:                img = open(imgName, O_RDONLY);
                    291:                if (img == -1) {
                    292:                        ESYSERR(0);
                    293:                        if (fd > 2)
                    294:                                close(fd);
                    295:                        return 3;
1.2       misho     296:                } else
1.6.2.6 ! misho     297:                        nameFlg = getFDtype(img);
        !           298: 
        !           299:                if (nameFlg & FD_SEEK) {
1.3       misho     300:                        iSize = lseek(img, 0, SEEK_END);
1.6.2.6 ! misho     301:                        if (!imgSize)
        !           302:                                imgSize = E_ALIGN(iSize, bufSize);
        !           303:                        if (iSize == -1 || iSize > imgSize) {
        !           304:                                if (fd > 2)
        !           305:                                        close(fd);
        !           306:                                close(img);
        !           307:                                EERROR(ENOSPC, "Error:: storage size %llu is "
        !           308:                                                "greater from file size %llu\n", 
        !           309:                                                iSize, imgSize);
        !           310:                                return 3;
        !           311:                        } else
        !           312:                                lseek(img, 0, SEEK_SET);
        !           313:                }
        !           314: 
        !           315:                if (!imgSize) {
1.3       misho     316:                        if (fd > 2)
                    317:                                close(fd);
1.6.2.6 ! misho     318:                        Usage();
        !           319:                        return 1;
        !           320:                }
1.2       misho     321:        }
                    322: 
1.3       misho     323:        if (!m) {
1.6.2.6 ! misho     324:                if (EmptyStore(img, nameFlg) == -1) {
1.3       misho     325:                        if (fd > 2)
                    326:                                close(fd);
                    327:                        close(img);
                    328:                        return 3;
                    329:                }
1.6.2.6 ! misho     330:                if (FillStore(img, nameFlg, fd, fileFlg) == -1) {
1.3       misho     331:                        if (fd > 2)
                    332:                                close(fd);
                    333:                        close(img);
                    334:                        return 4;
                    335:                }
                    336:        } else {        /* GET */
1.6.2.4   misho     337:                iSize ^= iSize;
1.6.2.6 ! misho     338:                if (EmptyStore(fd, fileFlg) == -1) {
1.3       misho     339:                        if (fd > 2)
                    340:                                close(fd);
                    341:                        close(img);
                    342:                        return 3;
                    343:                }
1.6.2.6 ! misho     344:                if (FillStore(fd, fileFlg, img, nameFlg) == -1) {
1.3       misho     345:                        if (fd > 2)
                    346:                                close(fd);
                    347:                        close(img);
                    348:                        return 4;
                    349:                }
1.2       misho     350:        }
                    351: 
                    352:        close(img);
                    353:        if (fd > 2)
                    354:                close(fd);
1.4       misho     355: 
                    356:        if (R)
                    357:                reboot(RB_AUTOBOOT);
1.2       misho     358:        return 0;
                    359: }

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