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

1.2       misho       1: /*************************************************************************
                      2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.3     ! misho       6:  * $Id: updimg.c,v 1.2.2.3 2012/04/05 12:22:44 misho Exp $
1.2       misho       7:  *
1.3     ! misho       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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
        !            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: */
1.2       misho      46: #include "global.h"
                     47: #include "upd.h"
                     48: 
                     49: 
                     50: int Verbose, Mode;
                     51: extern char compiled[], compiledby[], compilehost[];
                     52: 
                     53: 
1.3     ! misho      54: static void
        !            55: Usage()
1.2       misho      56: {
                     57:        printf( " Update Image tool for embedded systems on CompactFlash\n"
                     58:                "=== %s === %s@%s ===\n\n"
                     59:                "  Syntax: updimg [-v] <modes> <image_name> [operate_dir]\n\n"
                     60:                "\t-v\t\tVerbose ...\n\n"
                     61:                "\t-D\t\tRun tftp server and watch for updates (image_name==ip:port)\n"
                     62:                "\t-a\t\tMake image active for next boot\n"
                     63:                "\t-i <tftp_dir>\tInstall new image\n"
                     64:                "\t-r\t\tRollback old backuped image\n"
                     65:                "\t-t <tftp_dir>\tExport for TFTP download\n"
                     66:                "\t-b\t\tBackup image\n"
                     67:                "\t-d\t\tClean backuped image\n"
                     68:                "\n", compiled, compiledby, compilehost);
                     69: }
                     70: 
                     71: 
1.3     ! misho      72: int
        !            73: main(int argc, char **argv)
1.2       misho      74: {
                     75:        char ch, *pos, szImg[MAXPATHLEN], szTFTP[MAXPATHLEN];
                     76:        int mode;
                     77:        struct hostent *host;
                     78:        struct sockaddr_in sin;
                     79: 
                     80:        while ((ch = getopt(argc, argv, "hvdbt:ai:rD")) != -1)
                     81:                switch (ch) {
                     82:                        case 'D':
                     83:                                Mode |= 0x40;
                     84:                                break;
                     85:                        case 'a':
                     86:                                Mode |= 0x1;
                     87:                                break;
                     88:                        case 't':
                     89:                                Mode |= 0x8;
                     90:                                strlcpy(szTFTP, optarg, MAXPATHLEN);
                     91:                                break;
                     92:                        case 'r':
                     93:                                if (Mode & 0x10) {
                     94:                                        printf("Error:: can`t set rollback mode because find set install ...\n");
                     95:                                        return 1;
                     96:                                } else
                     97:                                        Mode |= 0x20;
                     98:                                break;
                     99:                        case 'i':
                    100:                                if (Mode & 0x20) {
                    101:                                        printf("Error:: can`t set install mode because find set rollback ...\n");
                    102:                                        return 1;
                    103:                                } else
                    104:                                        Mode |= 0x10;
                    105:                                strlcpy(szTFTP, optarg, MAXPATHLEN);
                    106:                                break;
                    107:                        case 'b':
                    108:                                if (Mode & 0x4) {
                    109:                                        printf("Error:: can`t set backup mode because find set clean ...\n");
                    110:                                        return 1;
                    111:                                } else
                    112:                                        Mode |= 0x2;
                    113:                                break;
                    114:                        case 'd':
                    115:                                if (Mode & 0x2) {
                    116:                                        printf("Error:: can`t set clean mode because find set backup ...\n");
                    117:                                        return 1;
                    118:                                } else
                    119:                                        Mode |= 0x4;
                    120:                                break;
                    121:                        case 'v':
                    122:                                Verbose++;
                    123:                                break;
                    124:                        case 'h':
                    125:                        default:
                    126:                                Usage();
                    127:                                return 1;
                    128:                }
                    129:        argc -= optind;
                    130:        argv += optind;
                    131:        if (!Mode) {
                    132:                printf("Error:: Mode not specified !!!\n\n");
                    133:                Usage();
                    134:                return 1;
                    135:        }
                    136:        if (!argc) {
                    137:                printf("Error:: Image filename not specified !!!\n\n");
                    138:                Usage();
                    139:                return 1;
                    140:        } else
                    141:                strlcpy(szImg, basename(*argv), MAXPATHLEN);
                    142:        if (0x40 & Mode) {
                    143:                if (0x40 != Mode) {
                    144:                        printf("Error:: Daemon mode can`t be specified with others ...\n");
                    145:                        return 1;
                    146:                }
                    147: 
                    148:                pos = strchr(szImg, ':');
                    149:                if (!pos)
                    150:                        sin.sin_port = htons(DEFAULT_TFTP);
                    151:                else {
                    152:                        *pos++ = 0;
                    153:                        sin.sin_port = htons(atoi(pos));
                    154:                        if (!sin.sin_port)
                    155:                                sin.sin_port = htons(DEFAULT_TFTP);
                    156:                }
                    157:                host = gethostbyname(szImg);
                    158:                if (!host) {
                    159:                        printf("Error:: in resolv host %s #%d - %s\n", szImg, h_errno, hstrerror(h_errno));
                    160:                        return 1;
                    161:                } else
                    162:                        memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
                    163: 
                    164:                strlcpy(szTFTP, !argv[1] ? DEFAULT_TFTPDIR : argv[1], MAXPATHLEN);
                    165:                chdir(argv[1]);
                    166:                VERB(2) printf("Info(2):: Host %s Port %d in Dir %s\n", 
                    167:                                inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), szTFTP);
                    168:        } else
                    169:                if (argc > 1) {
                    170:                        chdir(argv[1]);
                    171:                        VERB(5) printf("Info(5):: Change to dir %s\n", argv[1]);
                    172:                }
                    173: 
                    174:        openlog("updimg", LOG_CONS, 0);
                    175: 
                    176:        for (mode = 0x40; mode; mode >>= 1)
                    177:                switch (Mode & mode) {
                    178:                        case 0x1:
                    179:                                Activate(szImg);
                    180:                                break;
                    181:                        case 0x2:
                    182:                                Backup(szImg);
                    183:                                break;
                    184:                        case 0x4:
                    185:                                Clean(szImg);
                    186:                                break;
                    187:                        case 0x8:
                    188:                                tFTP(szImg, szTFTP);
                    189:                                break;
                    190:                        case 0x10:
                    191:                                Install(szImg, szTFTP);
                    192:                                break;
                    193:                        case 0x20:
                    194:                                Rollback(szImg);
                    195:                                break;
                    196:                        case 0x40:
                    197:                                Daemonize(sin, szTFTP);
                    198:                                break;
                    199:                }
                    200: 
                    201:        closelog();
                    202:        return 0;
                    203: }

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