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