1: /*************************************************************************
2: * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
6: * $Id: ube.c,v 1.1.2.10 2014/01/30 01:03:25 misho Exp $
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: #include "ub_env.h"
48:
49:
50: cfg_root_t cfg;
51: int Verbose;
52: extern char compiled[], compiledby[], compilehost[];
53:
54:
55: static void
56: Usage()
57: {
58: printf( " -= U-Boot-Env =- Tool for u-boot-env nand map management\n"
59: "=== %s === %s@%s ===\n\n"
60: " Syntax: ube [options] [set_value]\n"
61: "\n"
62: "\t-c <cfgfile>\tConfig file [default: /etc/ube.conf]\n"
63: "\t-d <descr>\tDrive description into config [default: 64K]\n"
64: "\t-g <name>\tSet parameter to value\n"
65: "\t-s <name>\tGet parameter value\n"
66: "\t-q\t\tQuiet mode\n"
67: "\t-v\t\tVerbose ...\n"
68: "\n", compiled, compiledby, compilehost);
69: }
70:
71: int
72: main(int argc, char **argv)
73: {
74: char ch, mode = 0, szName[STRSIZ] = { 0 }, szVal[STRSIZ] = { 0 },
75: szCfgName[PATH_MAX], szSec[STRSIZ];
76: const char *str;
77: int ret = 0;
78:
79: strlcpy(szCfgName, UBE_CFGNAME, sizeof szCfgName);
80: strlcpy(szSec, UBE_SECTION, sizeof szSec);
81: while ((ch = getopt(argc, argv, "hvqg:s:c:d:")) != -1)
82: switch (ch) {
83: case 'g':
84: mode |= 1;
85: strlcpy(szName, optarg, sizeof szName);
86: break;
87: case 's':
88: mode |= 2;
89: strlcpy(szName, optarg, sizeof szName);
90: break;
91: case 'c':
92: strlcpy(szCfgName, optarg, sizeof szCfgName);
93: break;
94: case 'd':
95: strlcpy(szSec, optarg, sizeof szSec);
96: break;
97: case 'v':
98: Verbose++;
99: break;
100: case 'q':
101: mode |= 0x80;
102: break;
103: case 'h':
104: default:
105: Usage();
106: return 1;
107: }
108: argc -= optind;
109: argv += optind;
110: if ((mode & 0x7f) == 2 && argc)
111: strlcpy(szVal, *argv, sizeof szVal);
112:
113: VERB(1) printf("u-boot-env: mode=0x%hhx name=%s value=%s\n", mode, szName, szVal);
114:
115: if (cfgLoadConfig(szCfgName, &cfg)) {
116: printf("Error:: cfgLoadConfig() #%d - %s\n", cfg_GetErrno(), cfg_GetError());
117: return 1;
118: }
119:
120: if (ub_load(szSec)) {
121: ret = 1;
122: goto end;
123: }
124:
125: if (!(mode & 0x7f)) {
126: VERB(2) printf("u-boot-env: list variables\n");
127: ub_env(szSec);
128: }
129:
130: if ((mode & 0x7f) & 1) {
131: VERB(2) printf("u-boot-env: get variable %s\n", szName);
132: str = ub_getenv(szSec, szName);
133: if (!str) {
134: printf("Error:: Variable %s not found!\n", szName);
135: ret = 2;
136: goto end;
137: } else if (mode & 0x80)
138: printf("%s\n", str);
139: else
140: printf("%s=%s\n", szName, str);
141: }
142:
143: if ((mode & 0x7f) & 2) {
144: VERB(2) printf("u-boot-env: set variable %s\n", szName);
145: ret = ub_setenv(szSec, szName, argc ? szVal : NULL);
146: if (ret) {
147: printf("Error:: Writing variable %s!\n", szName);
148: ret = 3;
149: goto end;
150: }
151: if (!(mode & 0x80))
152: printf("Done\n");
153: }
154:
155: end:
156: ub_unload();
157: cfgUnloadConfig(&cfg);
158: return ret;
159: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>