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

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.3     ! misho       6:  * $Id: imgupd.c,v 1.2.2.1 2014/02/05 21:59: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"
                     47: 
                     48: 
                     49: char imgName[PATH_MAX], imgFile[PATH_MAX];
                     50: off_t imgSize, iSize;
                     51: int Verbose;
                     52: extern char compiled[], compiledby[], compilehost[];
                     53: 
                     54: static void
                     55: Usage()
                     56: {
                     57: 
                     58:        printf( "IMGUPD is tool for management of images\n"
                     59:                "=== %s === %s@%s ===\n\n"
1.3     ! misho      60:                "  Syntax: imgupd [options] [image_file]\n\n"
1.2       misho      61:                "\t-v\t\tVerbose ...\n"
1.3     ! misho      62:                "\t-g\t\tGet image from Storage\n"
1.2       misho      63:                "\t-t\t\tTruncate Storage file name\n"
                     64:                "\t-s <size>\tStorage size (required for stdin)\n"
                     65:                "\t-f <devfile>\tStorage file name\n"
                     66:                "\n", compiled, compiledby, compilehost);
                     67: }
                     68: 
                     69: static int
                     70: EmptyStore(int img)
                     71: {
                     72:        register int i;
                     73:        u_char buf[IMGBUF_SIZE];
                     74:        ssize_t wlen;
                     75: 
                     76:        VERB(1) printf("Erase store %s\n", imgName);
                     77: 
                     78:        iSize = lseek(img, 0, SEEK_END);
                     79:        if (iSize == -1) {
                     80:                ESYSERR(0);
                     81:                return -1;
                     82:        } else
                     83:                imgSize += E_ALIGN(iSize, IMGBUF_SIZE);
                     84: 
                     85:        memset(buf, 0, sizeof buf);
                     86:        for (i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); i++)
                     87:                if ((wlen = write(img, buf, sizeof buf)) == -1 || 
                     88:                                wlen != sizeof buf) {
                     89:                        EERROR(EIO, "Error at chunk %d init %d bytes, should be %u\n", 
                     90:                                        i, wlen, sizeof buf);
                     91:                        return -1;
                     92:                } else
                     93:                        VERB(1) printf("+Written chunk #%d\n", i);
                     94: 
                     95:        iSize = lseek(img, iSize, SEEK_SET);
                     96:        return iSize;
                     97: }
                     98: 
                     99: static int
                    100: FillStore(int img, int fd)
                    101: {
                    102:        register int i, j;
                    103:        u_char buf[IMGBUF_SIZE];
                    104:        ssize_t rlen, wlen;
                    105: 
                    106:        VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
                    107: 
                    108:        for (j = 0, i = howmany(iSize, IMGBUF_SIZE); i < howmany(imgSize, IMGBUF_SIZE); 
                    109:                        i++, j++) {
                    110:                memset(buf, 0, sizeof buf);
                    111:                rlen = read(fd, buf, sizeof buf);
                    112:                if (rlen == -1) {
                    113:                        ESYSERR(0);
                    114:                        return -1;
                    115:                } else if (!rlen)
                    116:                        break;
                    117:                else
                    118:                        VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
                    119: 
                    120:                wlen = write(img, buf, rlen);
                    121:                if (wlen == -1) {
                    122:                        ESYSERR(0);
                    123:                        return -1;
                    124:                } else if (!wlen || wlen != rlen) {
                    125:                        EERROR(EIO, "Readed %d bytes are not equal to written %d bytes\n", 
                    126:                                        rlen, wlen);
                    127:                } else
                    128:                        VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
                    129:        }
                    130: 
                    131:        return 0;
                    132: }
                    133: 
                    134: int
                    135: main(int argc, char **argv)
                    136: {
1.3     ! misho     137:        char ch, m = 0;
1.2       misho     138:        int fd, img, tr = 0;
                    139: 
1.3     ! misho     140:        while ((ch = getopt(argc, argv, "hvgts:f:")) != -1)
1.2       misho     141:                switch (ch) {
                    142:                        case 'f':
                    143:                                strlcpy(imgName, optarg, sizeof imgName);
                    144:                                break;
                    145:                        case 's':
                    146:                                imgSize = strtoll(optarg, NULL, 0);
                    147:                                if (!imgSize) {
                    148:                                        Usage();
                    149:                                        return 1;
                    150:                                }
                    151:                                break;
                    152:                        case 't':
                    153:                                tr = O_TRUNC;
                    154:                                break;
1.3     ! misho     155:                        case 'g':
        !           156:                                m = 1;
        !           157:                                break;
1.2       misho     158:                        case 'v':
                    159:                                Verbose++;
                    160:                                break;
                    161:                        case 'h':
                    162:                        default:
                    163:                                Usage();
                    164:                                return 1;
                    165:                }
                    166:        argc -= optind;
                    167:        argv += optind;
                    168: 
1.3     ! misho     169:        if (!m) {
        !           170:                if (argc) {
        !           171:                        strlcpy(imgFile, *argv, sizeof imgFile);
        !           172:                        /* open image file */
        !           173:                        fd = open(imgFile, O_RDONLY);
        !           174:                        if (fd == -1) {
        !           175:                                ESYSERR(0);
        !           176:                                return 2;
        !           177:                        } else
        !           178:                                iSize = lseek(fd, 0, SEEK_END);
        !           179:                        if (!imgSize)
        !           180:                                imgSize = E_ALIGN(iSize, IMGBUF_SIZE);
        !           181:                        if (iSize == -1 || iSize > imgSize) {
        !           182:                                close(fd);
        !           183:                                EERROR(ENOSPC, "Error:: file size %llu is "
        !           184:                                                "greater from storage size %llu\n", 
        !           185:                                                iSize, imgSize);
        !           186:                                return 2;
        !           187:                        } else
        !           188:                                lseek(fd, 0, SEEK_SET);
        !           189:                } else if (!imgSize) {
        !           190:                        Usage();
        !           191:                        return 1;
        !           192:                } else
        !           193:                        fd = STDIN_FILENO;
        !           194:        } else {        /* GET */
        !           195:                if (argc) {
        !           196:                        strlcpy(imgFile, *argv, sizeof imgFile);
        !           197:                        /* open image file */
        !           198:                        fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
        !           199:                        if (fd == -1) {
        !           200:                                ESYSERR(0);
        !           201:                                return 2;
        !           202:                        }
        !           203:                } else if (!imgSize) {
        !           204:                        Usage();
        !           205:                        return 1;
        !           206:                } else
        !           207:                        fd = STDOUT_FILENO;
        !           208:        }
        !           209: 
        !           210:        VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
        !           211:                        imgSize, imgName, argc ? imgFile : "<stdin>");
        !           212: 
        !           213:        if (!m) {
        !           214:                /* open storage device */
        !           215:                img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
        !           216:                if (img == -1) {
1.2       misho     217:                        ESYSERR(0);
1.3     ! misho     218:                        if (fd > 2)
        !           219:                                close(fd);
        !           220:                        return 3;
        !           221:                }
        !           222:        } else {        /* GET */
        !           223:                /* open storage device */
        !           224:                img = open(imgName, O_RDONLY);
        !           225:                if (img == -1) {
        !           226:                        ESYSERR(0);
        !           227:                        if (fd > 2)
        !           228:                                close(fd);
        !           229:                        return 3;
1.2       misho     230:                } else
1.3     ! misho     231:                        iSize = lseek(img, 0, SEEK_END);
1.2       misho     232:                if (!imgSize)
                    233:                        imgSize = E_ALIGN(iSize, IMGBUF_SIZE);
                    234:                if (iSize == -1 || iSize > imgSize) {
1.3     ! misho     235:                        if (fd > 2)
        !           236:                                close(fd);
        !           237:                        close(img);
        !           238:                        EERROR(ENOSPC, "Error:: storage size %llu is "
        !           239:                                        "greater from file size %llu\n", 
1.2       misho     240:                                        iSize, imgSize);
1.3     ! misho     241:                        return 3;
1.2       misho     242:                } else
1.3     ! misho     243:                        lseek(img, 0, SEEK_SET);
1.2       misho     244:        }
                    245: 
1.3     ! misho     246:        if (!m) {
        !           247:                if (EmptyStore(img) == -1) {
        !           248:                        if (fd > 2)
        !           249:                                close(fd);
        !           250:                        close(img);
        !           251:                        return 3;
        !           252:                }
        !           253:                if (FillStore(img, fd) == -1) {
        !           254:                        if (fd > 2)
        !           255:                                close(fd);
        !           256:                        close(img);
        !           257:                        return 4;
        !           258:                }
        !           259:        } else {        /* GET */
        !           260:                if (EmptyStore(fd) == -1) {
        !           261:                        if (fd > 2)
        !           262:                                close(fd);
        !           263:                        close(img);
        !           264:                        return 3;
        !           265:                }
        !           266:                if (FillStore(fd, img) == -1) {
        !           267:                        if (fd > 2)
        !           268:                                close(fd);
        !           269:                        close(img);
        !           270:                        return 4;
        !           271:                }
1.2       misho     272:        }
                    273: 
                    274:        close(img);
                    275:        if (fd > 2)
                    276:                close(fd);
                    277:        return 0;
                    278: }

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