1: /*************************************************************************
2: * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
6: * $Id: updimg.c,v 1.2 2011/06/08 12:45:41 misho Exp $
7: *
8: *************************************************************************/
9: #include "global.h"
10: #include "upd.h"
11: #include "tftp.h"
12:
13:
14: sl_config cfg;
15: int Verbose, Mode;
16: extern char compiled[], compiledby[], compilehost[];
17:
18:
19: static void Usage()
20: {
21: printf( " Update Image tool for embedded systems on CompactFlash\n"
22: "=== %s === %s@%s ===\n\n"
23: " Syntax: updimg [-v] <modes> <image_name> [operate_dir]\n\n"
24: "\t-v\t\tVerbose ...\n\n"
25: "\t-D\t\tRun tftp server and watch for updates (image_name==ip:port)\n"
26: "\t-a\t\tMake image active for next boot\n"
27: "\t-i <tftp_dir>\tInstall new image\n"
28: "\t-r\t\tRollback old backuped image\n"
29: "\t-t <tftp_dir>\tExport for TFTP download\n"
30: "\t-b\t\tBackup image\n"
31: "\t-d\t\tClean backuped image\n"
32: "\n", compiled, compiledby, compilehost);
33: }
34:
35: // -----------------------------------
36:
37: int main(int argc, char **argv)
38: {
39: char ch, *pos, szImg[MAXPATHLEN], szTFTP[MAXPATHLEN];
40: int mode;
41: struct hostent *host;
42: struct sockaddr_in sin;
43:
44: while ((ch = getopt(argc, argv, "hvdbt:ai:rD")) != -1)
45: switch (ch) {
46: case 'D':
47: Mode |= 0x40;
48: break;
49: case 'a':
50: Mode |= 0x1;
51: break;
52: case 't':
53: Mode |= 0x8;
54: strlcpy(szTFTP, optarg, MAXPATHLEN);
55: break;
56: case 'r':
57: if (Mode & 0x10) {
58: printf("Error:: can`t set rollback mode because find set install ...\n");
59: return 1;
60: } else
61: Mode |= 0x20;
62: break;
63: case 'i':
64: if (Mode & 0x20) {
65: printf("Error:: can`t set install mode because find set rollback ...\n");
66: return 1;
67: } else
68: Mode |= 0x10;
69: strlcpy(szTFTP, optarg, MAXPATHLEN);
70: break;
71: case 'b':
72: if (Mode & 0x4) {
73: printf("Error:: can`t set backup mode because find set clean ...\n");
74: return 1;
75: } else
76: Mode |= 0x2;
77: break;
78: case 'd':
79: if (Mode & 0x2) {
80: printf("Error:: can`t set clean mode because find set backup ...\n");
81: return 1;
82: } else
83: Mode |= 0x4;
84: break;
85: case 'v':
86: Verbose++;
87: break;
88: case 'h':
89: default:
90: Usage();
91: return 1;
92: }
93: argc -= optind;
94: argv += optind;
95: if (!Mode) {
96: printf("Error:: Mode not specified !!!\n\n");
97: Usage();
98: return 1;
99: }
100: if (!argc) {
101: printf("Error:: Image filename not specified !!!\n\n");
102: Usage();
103: return 1;
104: } else
105: strlcpy(szImg, basename(*argv), MAXPATHLEN);
106: if (0x40 & Mode) {
107: if (0x40 != Mode) {
108: printf("Error:: Daemon mode can`t be specified with others ...\n");
109: return 1;
110: }
111:
112: pos = strchr(szImg, ':');
113: if (!pos)
114: sin.sin_port = htons(DEFAULT_TFTP);
115: else {
116: *pos++ = 0;
117: sin.sin_port = htons(atoi(pos));
118: if (!sin.sin_port)
119: sin.sin_port = htons(DEFAULT_TFTP);
120: }
121: host = gethostbyname(szImg);
122: if (!host) {
123: printf("Error:: in resolv host %s #%d - %s\n", szImg, h_errno, hstrerror(h_errno));
124: return 1;
125: } else
126: memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
127:
128: strlcpy(szTFTP, !argv[1] ? DEFAULT_TFTPDIR : argv[1], MAXPATHLEN);
129: chdir(argv[1]);
130: VERB(2) printf("Info(2):: Host %s Port %d in Dir %s\n",
131: inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), szTFTP);
132: } else
133: if (argc > 1) {
134: chdir(argv[1]);
135: VERB(5) printf("Info(5):: Change to dir %s\n", argv[1]);
136: }
137:
138: openlog("updimg", LOG_CONS, 0);
139:
140: for (mode = 0x40; mode; mode >>= 1)
141: switch (Mode & mode) {
142: case 0x1:
143: Activate(szImg);
144: break;
145: case 0x2:
146: Backup(szImg);
147: break;
148: case 0x4:
149: Clean(szImg);
150: break;
151: case 0x8:
152: tFTP(szImg, szTFTP);
153: break;
154: case 0x10:
155: Install(szImg, szTFTP);
156: break;
157: case 0x20:
158: Rollback(szImg);
159: break;
160: case 0x40:
161: Daemonize(sin, szTFTP);
162: break;
163: }
164:
165: closelog();
166: return 0;
167: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>