--- embedtools/src/Attic/updimg.c 2009/11/13 16:25:38 1.1.2.2 +++ embedtools/src/Attic/updimg.c 2012/07/22 22:46:48 1.3 @@ -1,68 +1,122 @@ +/************************************************************************* + * (C) 2010 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: updimg.c,v 1.3 2012/07/22 22:46:48 misho Exp $ + * + ************************************************************************* +The ELWIX and AITNET software is distributed under the following +terms: + +All of the documentation and software included in the ELWIX and AITNET +Releases is copyrighted by ELWIX - Sofia/Bulgaria + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" #include "upd.h" -sl_config cfg; int Verbose, Mode; extern char compiled[], compiledby[], compilehost[]; -static void Usage() +static void +Usage() { printf( " Update Image tool for embedded systems on CompactFlash\n" "=== %s === %s@%s ===\n\n" " Syntax: updimg [-v] [operate_dir]\n\n" "\t-v\t\tVerbose ...\n\n" + "\t-D\t\tRun tftp server and watch for updates (image_name==ip:port)\n" "\t-a\t\tMake image active for next boot\n" - "\t-i\t\tInstall new image\n" + "\t-i \tInstall new image\n" "\t-r\t\tRollback old backuped image\n" + "\t-t \tExport for TFTP download\n" "\t-b\t\tBackup image\n" "\t-d\t\tClean backuped image\n" "\n", compiled, compiledby, compilehost); } -// ----------------------------------- -int main(int argc, char **argv) +int +main(int argc, char **argv) { - char ch, szImg[MAXPATHLEN]; + char ch, *pos, szImg[MAXPATHLEN], szTFTP[MAXPATHLEN]; int mode; + struct hostent *host; + struct sockaddr_in sin; - while ((ch = getopt(argc, argv, "hvdbtair")) != -1) + while ((ch = getopt(argc, argv, "hvdbt:ai:rD")) != -1) switch (ch) { + case 'D': + Mode |= 0x40; + break; case 'a': Mode |= 0x1; break; case 't': Mode |= 0x8; + strlcpy(szTFTP, optarg, MAXPATHLEN); break; case 'r': - if (Mode & 0x2) { + if (Mode & 0x10) { printf("Error:: can`t set rollback mode because find set install ...\n"); return 1; } else - Mode |= 0x4; + Mode |= 0x20; break; case 'i': - if (Mode & 0x4) { + if (Mode & 0x20) { printf("Error:: can`t set install mode because find set rollback ...\n"); return 1; } else - Mode |= 0x2; + Mode |= 0x10; + strlcpy(szTFTP, optarg, MAXPATHLEN); break; case 'b': - if (Mode & 0x20) { + if (Mode & 0x4) { printf("Error:: can`t set backup mode because find set clean ...\n"); return 1; } else - Mode |= 0x10; + Mode |= 0x2; break; case 'd': - if (Mode & 0x10) { + if (Mode & 0x2) { printf("Error:: can`t set clean mode because find set backup ...\n"); return 1; } else - Mode |= 0x20; + Mode |= 0x4; break; case 'v': Verbose++; @@ -84,33 +138,63 @@ int main(int argc, char **argv) Usage(); return 1; } else - basename_r(*argv, szImg); - if (argc > 1) { + strlcpy(szImg, basename(*argv), MAXPATHLEN); + if (0x40 & Mode) { + if (0x40 != Mode) { + printf("Error:: Daemon mode can`t be specified with others ...\n"); + return 1; + } + + pos = strchr(szImg, ':'); + if (!pos) + sin.sin_port = htons(DEFAULT_TFTP); + else { + *pos++ = 0; + sin.sin_port = htons(atoi(pos)); + if (!sin.sin_port) + sin.sin_port = htons(DEFAULT_TFTP); + } + host = gethostbyname(szImg); + if (!host) { + printf("Error:: in resolv host %s #%d - %s\n", szImg, h_errno, hstrerror(h_errno)); + return 1; + } else + memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length); + + strlcpy(szTFTP, !argv[1] ? DEFAULT_TFTPDIR : argv[1], MAXPATHLEN); chdir(argv[1]); - VERB(5) printf("Info(5):: Change to dir %s\n", argv[1]); - } + VERB(2) printf("Info(2):: Host %s Port %d in Dir %s\n", + inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), szTFTP); + } else + if (argc > 1) { + chdir(argv[1]); + VERB(5) printf("Info(5):: Change to dir %s\n", argv[1]); + } openlog("updimg", LOG_CONS, 0); - for (mode = 0x20; mode; mode >>= 1) + for (mode = 0x40; mode; mode >>= 1) switch (Mode & mode) { case 0x1: Activate(szImg); break; case 0x2: - Install(); + Backup(szImg); break; case 0x4: - Rollback(); + Clean(szImg); break; case 0x8: - tFTP(); + tFTP(szImg, szTFTP); break; case 0x10: - Backup(); + Install(szImg, szTFTP); break; case 0x20: - Clean(); + Rollback(szImg); + break; + case 0x40: + Daemonize(sin, szTFTP); break; }