nvram: fix memory leak
[openwrt/openwrt.git] / package / utils / nvram / src / cli.c
1 /*
2 * Command line interface for libnvram
3 *
4 * Copyright 2009, Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 *
21 * The libnvram code is based on Broadcom code for Linux 2.4.x .
22 *
23 */
24
25 #include "nvram.h"
26
27
28 static nvram_handle_t * nvram_open_rdonly(void)
29 {
30 char *file = nvram_find_staging();
31
32 if( file == NULL )
33 file = nvram_find_mtd();
34
35 if( file != NULL ) {
36 nvram_handle_t *h = nvram_open(file, NVRAM_RO);
37 if( strcmp(file, NVRAM_STAGING) )
38 free(file);
39 return h;
40 }
41
42 return NULL;
43 }
44
45 static nvram_handle_t * nvram_open_staging(void)
46 {
47 if( nvram_find_staging() != NULL || nvram_to_staging() == 0 )
48 return nvram_open(NVRAM_STAGING, NVRAM_RW);
49
50 return NULL;
51 }
52
53 static int do_show(nvram_handle_t *nvram)
54 {
55 nvram_tuple_t *t;
56 int stat = 1;
57
58 if( (t = nvram_getall(nvram)) != NULL )
59 {
60 while( t )
61 {
62 printf("%s=%s\n", t->name, t->value);
63 t = t->next;
64 }
65
66 stat = 0;
67 }
68
69 return stat;
70 }
71
72 static int do_get(nvram_handle_t *nvram, const char *var)
73 {
74 const char *val;
75 int stat = 1;
76
77 if( (val = nvram_get(nvram, var)) != NULL )
78 {
79 printf("%s\n", val);
80 stat = 0;
81 }
82
83 return stat;
84 }
85
86 static int do_unset(nvram_handle_t *nvram, const char *var)
87 {
88 return nvram_unset(nvram, var);
89 }
90
91 static int do_set(nvram_handle_t *nvram, const char *pair)
92 {
93 char *val = strstr(pair, "=");
94 char var[strlen(pair)];
95 int stat = 1;
96
97 if( val != NULL )
98 {
99 memset(var, 0, sizeof(var));
100 strncpy(var, pair, (int)(val-pair));
101 stat = nvram_set(nvram, var, (char *)(val + 1));
102 }
103
104 return stat;
105 }
106
107 static int do_info(nvram_handle_t *nvram)
108 {
109 nvram_header_t *hdr = nvram_header(nvram);
110
111 /* CRC8 over the last 11 bytes of the header and data bytes */
112 uint8_t crc = hndcrc8((unsigned char *) &hdr[0] + NVRAM_CRC_START_POSITION,
113 hdr->len - NVRAM_CRC_START_POSITION, 0xff);
114
115 /* Show info */
116 printf("Magic: 0x%08X\n", hdr->magic);
117 printf("Length: 0x%08X\n", hdr->len);
118 printf("Offset: 0x%08X\n", nvram->offset);
119
120 printf("CRC8: 0x%02X (calculated: 0x%02X)\n",
121 hdr->crc_ver_init & 0xFF, crc);
122
123 printf("Version: 0x%02X\n", (hdr->crc_ver_init >> 8) & 0xFF);
124 printf("SDRAM init: 0x%04X\n", (hdr->crc_ver_init >> 16) & 0xFFFF);
125 printf("SDRAM config: 0x%04X\n", hdr->config_refresh & 0xFFFF);
126 printf("SDRAM refresh: 0x%04X\n", (hdr->config_refresh >> 16) & 0xFFFF);
127 printf("NCDL values: 0x%08X\n\n", hdr->config_ncdl);
128
129 printf("%i bytes used / %i bytes available (%.2f%%)\n",
130 hdr->len, nvram->length - nvram->offset - hdr->len,
131 (100.00 / (double)(nvram->length - nvram->offset)) * (double)hdr->len);
132
133 return 0;
134 }
135
136
137 int main( int argc, const char *argv[] )
138 {
139 nvram_handle_t *nvram;
140 int commit = 0;
141 int write = 0;
142 int stat = 1;
143 int done = 0;
144 int i;
145
146 /* Ugly... iterate over arguments to see whether we can expect a write */
147 for( i = 1; i < argc; i++ )
148 if( ( !strcmp(argv[i], "set") && ++i < argc ) ||
149 ( !strcmp(argv[i], "unset") && ++i < argc ) ||
150 !strcmp(argv[i], "commit") )
151 {
152 write = 1;
153 break;
154 }
155
156
157 nvram = write ? nvram_open_staging() : nvram_open_rdonly();
158
159 if( nvram != NULL && argc > 1 )
160 {
161 for( i = 1; i < argc; i++ )
162 {
163 if( !strcmp(argv[i], "show") )
164 {
165 stat = do_show(nvram);
166 done++;
167 }
168 else if( !strcmp(argv[i], "info") )
169 {
170 stat = do_info(nvram);
171 done++;
172 }
173 else if( !strcmp(argv[i], "get") || !strcmp(argv[i], "unset") || !strcmp(argv[i], "set") )
174 {
175 if( (i+1) < argc )
176 {
177 switch(argv[i++][0])
178 {
179 case 'g':
180 stat = do_get(nvram, argv[i]);
181 break;
182
183 case 'u':
184 stat = do_unset(nvram, argv[i]);
185 break;
186
187 case 's':
188 stat = do_set(nvram, argv[i]);
189 break;
190 }
191 done++;
192 }
193 else
194 {
195 fprintf(stderr, "Command '%s' requires an argument!\n", argv[i]);
196 done = 0;
197 break;
198 }
199 }
200 else if( !strcmp(argv[i], "commit") )
201 {
202 commit = 1;
203 done++;
204 }
205 else
206 {
207 fprintf(stderr, "Unknown option '%s' !\n", argv[i]);
208 done = 0;
209 break;
210 }
211 }
212
213 if( write )
214 stat = nvram_commit(nvram);
215
216 nvram_close(nvram);
217
218 if( commit )
219 stat = staging_to_nvram();
220 }
221
222 if( !nvram )
223 {
224 fprintf(stderr,
225 "Could not open nvram! Possible reasons are:\n"
226 " - No device found (/proc not mounted or no nvram present)\n"
227 " - Insufficient permissions to open mtd device\n"
228 " - Insufficient memory to complete operation\n"
229 " - Memory mapping failed or not supported\n"
230 );
231
232 stat = 1;
233 }
234 else if( !done )
235 {
236 fprintf(stderr,
237 "Usage:\n"
238 " nvram show\n"
239 " nvram info\n"
240 " nvram get variable\n"
241 " nvram set variable=value [set ...]\n"
242 " nvram unset variable [unset ...]\n"
243 " nvram commit\n"
244 );
245
246 stat = 1;
247 }
248
249 return stat;
250 }