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

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.8     ! misho       6:  * $Id: imgupd.c,v 1.7.4.1 2017/01/22 20:40:10 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: 
1.8     ! misho      15: Copyright 2004 - 2017
1.2       misho      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.7       misho      47: #include "imgupd.h"
1.2       misho      48: 
                     49: 
                     50: char imgName[PATH_MAX], imgFile[PATH_MAX];
1.7       misho      51: off_t imgSize, iSize, fromBegin;
                     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"
1.7       misho      64:                "\t-i\t\tStart fill storage from begin\n"
1.3       misho      65:                "\t-g\t\tGet image from Storage\n"
1.2       misho      66:                "\t-t\t\tTruncate Storage file name\n"
                     67:                "\t-s <size>\tStorage size (required for stdin)\n"
                     68:                "\t-f <devfile>\tStorage file name\n"
                     69:                "\n", compiled, compiledby, compilehost);
                     70: }
                     71: 
                     72: static int
1.7       misho      73: getFDtype(int fd)
                     74: {
                     75:        int type, flg = 0;
                     76:        struct stat sb;
                     77: 
                     78:        if (fstat(fd, &sb) == -1) {
                     79:                ESYSERR(0);
                     80:                return -1;
                     81:        }
                     82:        if (S_ISREG(sb.st_mode))
                     83:                flg |= FD_TRUNC;
                     84:        if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
                     85:                if (ioctl(fd, FIODTYPE, &type) == -1) {
                     86:                        ESYSERR(0);
                     87:                        return -1;
                     88:                }
                     89:                if (type & D_TAPE)
                     90:                        flg |= FD_TAPE;
                     91:                else if (type & (D_DISK | D_MEM))
                     92:                        flg |= FD_SEEK;
                     93:                if (S_ISCHR(sb.st_mode) && !(type & D_TAPE))
                     94:                        flg |= FD_CHR;
                     95:                goto end;
                     96:        }
                     97:        errno ^= errno;
                     98:        if (lseek(fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
                     99:                flg |= FD_PIPE;
                    100:        else
                    101:                flg |= FD_SEEK;
                    102: end:
                    103:        return flg;
                    104: }
                    105: 
                    106: static int
                    107: EmptyStore(int img, int flg)
1.2       misho     108: {
                    109:        register int i;
1.4       misho     110:        u_char buf[bufSize];
1.2       misho     111:        ssize_t wlen;
                    112: 
                    113:        VERB(1) printf("Erase store %s\n", imgName);
                    114: 
1.7       misho     115:        if (!fromBegin && flg & FD_SEEK) {
                    116:                iSize = lseek(img, 0, SEEK_END);
                    117:                if (iSize == -1) {
                    118:                        ESYSERR(0);
                    119:                        return -1;
                    120:                } else
                    121:                        imgSize += E_ALIGN(iSize, bufSize);
1.2       misho     122:        } else
1.7       misho     123:                iSize ^= iSize;
1.2       misho     124: 
                    125:        memset(buf, 0, sizeof buf);
1.4       misho     126:        for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++)
1.5       misho     127:                if ((wlen = write(img, buf, bufSize)) == -1 || 
                    128:                                (wlen && wlen != bufSize)) {
1.7       misho     129:                        if (wlen == -1)
                    130:                                ESYSERR(0);
                    131:                        else
1.8     ! misho     132:                                EERROR(EIO, "Error at chunk %d init %zd bytes, "
        !           133:                                                "should be %d\n", i, wlen, bufSize);
1.2       misho     134:                        return -1;
                    135:                } else
                    136:                        VERB(1) printf("+Written chunk #%d\n", i);
                    137: 
1.7       misho     138:        if (flg & FD_SEEK)
                    139:                iSize = lseek(img, iSize, SEEK_SET);
1.2       misho     140:        return iSize;
                    141: }
                    142: 
                    143: static int
1.7       misho     144: FillStore(int img, int iflg, int fd, int fflg)
1.2       misho     145: {
                    146:        register int i, j;
1.7       misho     147:        u_char *pos, buf[bufSize];
                    148:        ssize_t rlen, wlen, len;
1.2       misho     149: 
                    150:        VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
                    151: 
1.4       misho     152:        for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); 
1.2       misho     153:                        i++, j++) {
                    154:                memset(buf, 0, sizeof buf);
1.7       misho     155:                for (len = bufSize, pos = buf; len > 0; len -= rlen, pos += rlen) {
                    156:                        rlen = read(fd, pos, len);
                    157:                        if (rlen == -1) {
                    158:                                ESYSERR(0);
                    159:                                return -1;
                    160:                        } else if (!rlen)
                    161:                                break;
                    162:                        else
1.8     ! misho     163:                                VERB(1) printf("+Readed %zd bytes for chunk #%d\n", rlen, j);
1.7       misho     164:                }
1.2       misho     165: 
1.7       misho     166:                wlen = write(img, buf, bufSize);
1.2       misho     167:                if (wlen == -1) {
                    168:                        ESYSERR(0);
                    169:                        return -1;
1.7       misho     170:                } else if (wlen && wlen != bufSize) {
1.8     ! misho     171:                        EERROR(EIO, "Error at chunk %d written %zd bytes, "
1.7       misho     172:                                        "should be %u\n", i, wlen, bufSize);
1.2       misho     173:                } else
1.8     ! misho     174:                        VERB(1) printf("+Written %zd bytes at chunk #%d\n", wlen, i);
1.2       misho     175:        }
                    176: 
                    177:        return 0;
                    178: }
                    179: 
                    180: int
                    181: main(int argc, char **argv)
                    182: {
1.4       misho     183:        char ch, m = 0, R = 0;
1.2       misho     184:        int fd, img, tr = 0;
                    185: 
1.7       misho     186:        while ((ch = getopt(argc, argv, "hvRigts:f:")) != -1)
1.2       misho     187:                switch (ch) {
                    188:                        case 'f':
                    189:                                strlcpy(imgName, optarg, sizeof imgName);
                    190:                                break;
                    191:                        case 's':
                    192:                                imgSize = strtoll(optarg, NULL, 0);
                    193:                                if (!imgSize) {
                    194:                                        Usage();
                    195:                                        return 1;
                    196:                                }
                    197:                                break;
                    198:                        case 't':
                    199:                                tr = O_TRUNC;
                    200:                                break;
1.3       misho     201:                        case 'g':
                    202:                                m = 1;
1.7       misho     203:                        case 'i':
                    204:                                fromBegin = 1;
1.4       misho     205:                                break;
                    206:                        case 'R':
                    207:                                R = 1;
                    208:                                break;
1.2       misho     209:                        case 'v':
                    210:                                Verbose++;
                    211:                                break;
                    212:                        case 'h':
                    213:                        default:
                    214:                                Usage();
                    215:                                return 1;
                    216:                }
                    217:        argc -= optind;
                    218:        argv += optind;
                    219: 
1.3       misho     220:        if (!m) {
                    221:                if (argc) {
                    222:                        strlcpy(imgFile, *argv, sizeof imgFile);
                    223:                        /* open image file */
                    224:                        fd = open(imgFile, O_RDONLY);
                    225:                        if (fd == -1) {
                    226:                                ESYSERR(0);
                    227:                                return 2;
                    228:                        } else
1.7       misho     229:                                fileFlg = getFDtype(fd);
                    230: 
                    231:                        if (fileFlg & FD_SEEK) {
1.3       misho     232:                                iSize = lseek(fd, 0, SEEK_END);
1.7       misho     233:                                if (!imgSize)
                    234:                                        imgSize = E_ALIGN(iSize, bufSize);
                    235:                                if (iSize == -1 || iSize > imgSize) {
                    236:                                        close(fd);
                    237:                                        EERROR(ENOSPC, "Error:: file size %llu is "
                    238:                                                        "greater from storage size %llu\n", 
1.8     ! misho     239:                                                        (unsigned long long) iSize, 
        !           240:                                                        (unsigned long long) imgSize);
1.7       misho     241:                                        return 2;
                    242:                                } else
                    243:                                        lseek(fd, 0, SEEK_SET);
                    244:                        }
                    245:                } else {
                    246:                        fd = STDIN_FILENO;
                    247:                        fileFlg = getFDtype(fd);
                    248:                }
                    249: 
                    250:                if (!imgSize) {
                    251:                        if (fd > 2)
1.3       misho     252:                                close(fd);
                    253:                        Usage();
                    254:                        return 1;
1.7       misho     255:                }
1.3       misho     256:        } else {        /* GET */
                    257:                if (argc) {
                    258:                        strlcpy(imgFile, *argv, sizeof imgFile);
                    259:                        /* open image file */
                    260:                        fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
                    261:                        if (fd == -1) {
                    262:                                ESYSERR(0);
                    263:                                return 2;
1.7       misho     264:                        } else
                    265:                                fileFlg = getFDtype(fd);
                    266:                } else {
1.3       misho     267:                        fd = STDOUT_FILENO;
1.7       misho     268:                        fileFlg = getFDtype(fd);
                    269:                }
1.3       misho     270:        }
                    271: 
                    272:        VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
1.8     ! misho     273:                        (unsigned long long) imgSize, imgName, argc ? imgFile : "<stdin>");
1.3       misho     274: 
                    275:        if (!m) {
                    276:                /* open storage device */
                    277:                img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
                    278:                if (img == -1) {
1.2       misho     279:                        ESYSERR(0);
1.3       misho     280:                        if (fd > 2)
                    281:                                close(fd);
                    282:                        return 3;
1.7       misho     283:                } else
                    284:                        nameFlg = getFDtype(img);
1.3       misho     285:        } else {        /* GET */
                    286:                /* open storage device */
                    287:                img = open(imgName, O_RDONLY);
                    288:                if (img == -1) {
                    289:                        ESYSERR(0);
                    290:                        if (fd > 2)
                    291:                                close(fd);
                    292:                        return 3;
1.2       misho     293:                } else
1.7       misho     294:                        nameFlg = getFDtype(img);
                    295: 
                    296:                if (nameFlg & FD_SEEK) {
1.3       misho     297:                        iSize = lseek(img, 0, SEEK_END);
1.7       misho     298:                        if (!imgSize)
                    299:                                imgSize = E_ALIGN(iSize, bufSize);
                    300:                        if (iSize == -1 || iSize > imgSize) {
                    301:                                if (fd > 2)
                    302:                                        close(fd);
                    303:                                close(img);
                    304:                                EERROR(ENOSPC, "Error:: storage size %llu is "
                    305:                                                "greater from file size %llu\n", 
1.8     ! misho     306:                                                (unsigned long long) iSize, 
        !           307:                                                (unsigned long long) imgSize);
1.7       misho     308:                                return 3;
                    309:                        } else
                    310:                                lseek(img, 0, SEEK_SET);
                    311:                }
                    312: 
                    313:                if (!imgSize) {
1.3       misho     314:                        if (fd > 2)
                    315:                                close(fd);
1.7       misho     316:                        Usage();
                    317:                        return 1;
                    318:                }
1.2       misho     319:        }
                    320: 
1.3       misho     321:        if (!m) {
1.7       misho     322:                if (EmptyStore(img, nameFlg) == -1) {
1.3       misho     323:                        if (fd > 2)
                    324:                                close(fd);
                    325:                        close(img);
                    326:                        return 3;
                    327:                }
1.7       misho     328:                if (FillStore(img, nameFlg, fd, fileFlg) == -1) {
1.3       misho     329:                        if (fd > 2)
                    330:                                close(fd);
                    331:                        close(img);
                    332:                        return 4;
                    333:                }
                    334:        } else {        /* GET */
1.7       misho     335:                iSize ^= iSize;
                    336:                if (!(fileFlg & FD_PIPE))
                    337:                        if (EmptyStore(fd, fileFlg) == -1) {
                    338:                                if (fd > 2)
                    339:                                        close(fd);
                    340:                                close(img);
                    341:                                return 3;
                    342:                        }
                    343:                if (FillStore(fd, fileFlg, img, nameFlg) == -1) {
1.3       misho     344:                        if (fd > 2)
                    345:                                close(fd);
                    346:                        close(img);
                    347:                        return 4;
                    348:                }
1.2       misho     349:        }
                    350: 
                    351:        close(img);
                    352:        if (fd > 2)
                    353:                close(fd);
1.4       misho     354: 
                    355:        if (R)
                    356:                reboot(RB_AUTOBOOT);
1.2       misho     357:        return 0;
                    358: }

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