Annotation of embedtools/src/imgupd.c, revision 1.6.2.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.6.2.3 ! misho       6:  * $Id: imgupd.c,v 1.6.2.2 2014/02/06 00:16:05 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];
1.6.2.1   misho      50: off_t imgSize, iSize, fromBegin;
1.4       misho      51: int Verbose, bufSize = IMGBUF_SIZE;
1.2       misho      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.4       misho      62:                "\t-R\t\tReboot system after complete\n"
                     63:                "\t-p\t\tPipe suitable transfer on little chunks\n"
1.6.2.1   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
                     73: EmptyStore(int img)
                     74: {
                     75:        register int i;
1.4       misho      76:        u_char buf[bufSize];
1.2       misho      77:        ssize_t wlen;
                     78: 
                     79:        VERB(1) printf("Erase store %s\n", imgName);
                     80: 
1.6.2.1   misho      81:        if (!fromBegin) {
                     82:                iSize = lseek(img, 0, SEEK_END);
                     83:                if (iSize == -1) {
                     84:                        ESYSERR(0);
                     85:                        return -1;
                     86:                } else
                     87:                        imgSize += E_ALIGN(iSize, bufSize);
1.2       misho      88:        } else
1.6.2.1   misho      89:                iSize ^= iSize;
1.2       misho      90: 
                     91:        memset(buf, 0, sizeof buf);
1.4       misho      92:        for (i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); i++)
1.5       misho      93:                if ((wlen = write(img, buf, bufSize)) == -1 || 
                     94:                                (wlen && wlen != bufSize)) {
                     95:                        EERROR(EIO, "Error at chunk %d init %d bytes, "
                     96:                                        "should be %u\n", i, wlen, bufSize);
1.2       misho      97:                        return -1;
                     98:                } else
                     99:                        VERB(1) printf("+Written chunk #%d\n", i);
                    100: 
                    101:        iSize = lseek(img, iSize, SEEK_SET);
                    102:        return iSize;
                    103: }
                    104: 
                    105: static int
                    106: FillStore(int img, int fd)
                    107: {
                    108:        register int i, j;
1.4       misho     109:        u_char buf[bufSize];
1.2       misho     110:        ssize_t rlen, wlen;
                    111: 
                    112:        VERB(1) printf("Fill store %s from image file %s\n", imgName, imgFile);
                    113: 
1.4       misho     114:        for (j = 0, i = howmany(iSize, bufSize); i < howmany(imgSize, bufSize); 
1.2       misho     115:                        i++, j++) {
                    116:                memset(buf, 0, sizeof buf);
1.5       misho     117:                rlen = read(fd, buf, bufSize);
1.2       misho     118:                if (rlen == -1) {
                    119:                        ESYSERR(0);
                    120:                        return -1;
                    121:                } else if (!rlen)
                    122:                        break;
                    123:                else
                    124:                        VERB(1) printf("+Readed %d bytes for chunk #%d\n", rlen, j);
                    125: 
1.6.2.3 ! misho     126:                wlen = write(img, buf, bufSize);
1.2       misho     127:                if (wlen == -1) {
                    128:                        ESYSERR(0);
                    129:                        return -1;
1.6.2.3 ! misho     130:                } else if (wlen && wlen != bufSize) {
        !           131:                        EERROR(EIO, "Error at chunk %d written %d bytes, "
        !           132:                                        "should be %u\n", i, wlen, bufSize);
1.2       misho     133:                } else
                    134:                        VERB(1) printf("+Written %d bytes at chunk #%d\n", wlen, i);
                    135:        }
                    136: 
                    137:        return 0;
                    138: }
                    139: 
                    140: int
                    141: main(int argc, char **argv)
                    142: {
1.4       misho     143:        char ch, m = 0, R = 0;
1.2       misho     144:        int fd, img, tr = 0;
                    145: 
1.6.2.1   misho     146:        while ((ch = getopt(argc, argv, "hvRipgts:f:")) != -1)
1.2       misho     147:                switch (ch) {
                    148:                        case 'f':
                    149:                                strlcpy(imgName, optarg, sizeof imgName);
                    150:                                break;
                    151:                        case 's':
                    152:                                imgSize = strtoll(optarg, NULL, 0);
                    153:                                if (!imgSize) {
                    154:                                        Usage();
                    155:                                        return 1;
                    156:                                }
                    157:                                break;
                    158:                        case 't':
                    159:                                tr = O_TRUNC;
                    160:                                break;
1.4       misho     161:                        case 'p':
                    162:                                bufSize = IMGBUF_SIZE2;
                    163:                                break;
1.6.2.2   misho     164:                        case 'g':
                    165:                                m = 1;
1.6.2.1   misho     166:                        case 'i':
                    167:                                fromBegin = 1;
                    168:                                break;
1.4       misho     169:                        case 'R':
                    170:                                R = 1;
                    171:                                break;
1.2       misho     172:                        case 'v':
                    173:                                Verbose++;
                    174:                                break;
                    175:                        case 'h':
                    176:                        default:
                    177:                                Usage();
                    178:                                return 1;
                    179:                }
                    180:        argc -= optind;
                    181:        argv += optind;
                    182: 
1.3       misho     183:        if (!m) {
                    184:                if (argc) {
                    185:                        strlcpy(imgFile, *argv, sizeof imgFile);
                    186:                        /* open image file */
                    187:                        fd = open(imgFile, O_RDONLY);
                    188:                        if (fd == -1) {
                    189:                                ESYSERR(0);
                    190:                                return 2;
                    191:                        } else
                    192:                                iSize = lseek(fd, 0, SEEK_END);
                    193:                        if (!imgSize)
1.4       misho     194:                                imgSize = E_ALIGN(iSize, bufSize);
1.3       misho     195:                        if (iSize == -1 || iSize > imgSize) {
                    196:                                close(fd);
                    197:                                EERROR(ENOSPC, "Error:: file size %llu is "
                    198:                                                "greater from storage size %llu\n", 
                    199:                                                iSize, imgSize);
                    200:                                return 2;
                    201:                        } else
                    202:                                lseek(fd, 0, SEEK_SET);
                    203:                } else if (!imgSize) {
                    204:                        Usage();
                    205:                        return 1;
                    206:                } else
                    207:                        fd = STDIN_FILENO;
                    208:        } else {        /* GET */
                    209:                if (argc) {
                    210:                        strlcpy(imgFile, *argv, sizeof imgFile);
                    211:                        /* open image file */
                    212:                        fd = open(imgFile, O_WRONLY | O_TRUNC | O_CREAT, 0644);
                    213:                        if (fd == -1) {
                    214:                                ESYSERR(0);
                    215:                                return 2;
                    216:                        }
                    217:                } else if (!imgSize) {
                    218:                        Usage();
                    219:                        return 1;
                    220:                } else
                    221:                        fd = STDOUT_FILENO;
                    222:        }
                    223: 
                    224:        VERB(1) printf("imgSize=%llu imgName=%s imgFile=%s\n", 
                    225:                        imgSize, imgName, argc ? imgFile : "<stdin>");
                    226: 
                    227:        if (!m) {
                    228:                /* open storage device */
                    229:                img = open(imgName, O_RDWR | O_CREAT | tr, 0644);
                    230:                if (img == -1) {
1.2       misho     231:                        ESYSERR(0);
1.3       misho     232:                        if (fd > 2)
                    233:                                close(fd);
                    234:                        return 3;
                    235:                }
                    236:        } else {        /* GET */
                    237:                /* open storage device */
                    238:                img = open(imgName, O_RDONLY);
                    239:                if (img == -1) {
                    240:                        ESYSERR(0);
                    241:                        if (fd > 2)
                    242:                                close(fd);
                    243:                        return 3;
1.2       misho     244:                } else
1.3       misho     245:                        iSize = lseek(img, 0, SEEK_END);
1.2       misho     246:                if (!imgSize)
1.4       misho     247:                        imgSize = E_ALIGN(iSize, bufSize);
1.2       misho     248:                if (iSize == -1 || iSize > imgSize) {
1.3       misho     249:                        if (fd > 2)
                    250:                                close(fd);
                    251:                        close(img);
                    252:                        EERROR(ENOSPC, "Error:: storage size %llu is "
                    253:                                        "greater from file size %llu\n", 
1.2       misho     254:                                        iSize, imgSize);
1.3       misho     255:                        return 3;
1.2       misho     256:                } else
1.3       misho     257:                        lseek(img, 0, SEEK_SET);
1.2       misho     258:        }
                    259: 
1.3       misho     260:        if (!m) {
                    261:                if (EmptyStore(img) == -1) {
                    262:                        if (fd > 2)
                    263:                                close(fd);
                    264:                        close(img);
                    265:                        return 3;
                    266:                }
                    267:                if (FillStore(img, fd) == -1) {
                    268:                        if (fd > 2)
                    269:                                close(fd);
                    270:                        close(img);
                    271:                        return 4;
                    272:                }
                    273:        } else {        /* GET */
                    274:                if (EmptyStore(fd) == -1) {
                    275:                        if (fd > 2)
                    276:                                close(fd);
                    277:                        close(img);
                    278:                        return 3;
                    279:                }
                    280:                if (FillStore(fd, img) == -1) {
                    281:                        if (fd > 2)
                    282:                                close(fd);
                    283:                        close(img);
                    284:                        return 4;
                    285:                }
1.2       misho     286:        }
                    287: 
                    288:        close(img);
                    289:        if (fd > 2)
                    290:                close(fd);
1.4       misho     291: 
                    292:        if (R)
                    293:                reboot(RB_AUTOBOOT);
1.2       misho     294:        return 0;
                    295: }

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