kernel: the interface fsync changed a little
[openwrt.git] / package / 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 const char *file = nvram_find_staging();
31
32 if( file == NULL )
33 file = nvram_find_mtd();
34
35 if( file != NULL )
36 return nvram_open(file, NVRAM_RO);
37
38 return NULL;
39 }
40
41 static nvram_handle_t * nvram_open_staging(void)
42 {
43 if( nvram_find_staging() != NULL || nvram_to_staging() == 0 )
44 return nvram_open(NVRAM_STAGING, NVRAM_RW);
45
46 return NULL;
47 }
48
49 static int do_show(nvram_handle_t *nvram)
50 {
51 nvram_tuple_t *t;
52 int stat = 1;
53
54 if( (t = nvram_getall(nvram)) != NULL )
55 {
56 while( t )
57 {
58 printf("%s=%s\n", t->name, t->value);
59 t = t->next;
60 }
61
62 stat = 0;
63 }
64
65 return stat;
66 }
67
68 static int do_get(nvram_handle_t *nvram, const char *var)
69 {
70 const char *val;
71 int stat = 1;
72
73 if( (val = nvram_get(nvram, var)) != NULL )
74 {
75 printf("%s\n", val);
76 stat = 0;
77 }
78
79 return stat;
80 }
81
82 static int do_unset(nvram_handle_t *nvram, const char *var)
83 {
84 return nvram_unset(nvram, var);
85 }
86
87 static int do_set(nvram_handle_t *nvram, const char *pair)
88 {
89 char *val = strstr(pair, "=");
90 char var[strlen(pair)];
91 int stat = 1;
92
93 if( val != NULL )
94 {
95 memset(var, 0, sizeof(var));
96 strncpy(var, pair, (int)(val-pair));
97 stat = nvram_set(nvram, var, (char *)(val + 1));
98 }
99
100 return stat;
101 }
102
103 static int do_info(nvram_handle_t *nvram)
104 {
105 nvram_header_t *hdr = nvram_header(nvram);
106
107 /* CRC8 over the last 11 bytes of the header and data bytes */
108 uint8_t crc = hndcrc8((unsigned char *) &hdr[0] + NVRAM_CRC_START_POSITION,
109 hdr->len - NVRAM_CRC_START_POSITION, 0xff);
110
111 /* Show info */
112 printf("Magic: 0x%08X\n", hdr->magic);
113 printf("Length: 0x%08X\n", hdr->len);
114
115 printf("CRC8: 0x%02X (calculated: 0x%02X)\n",
116 hdr->crc_ver_init & 0xFF, crc);
117
118 printf("Version: 0x%02X\n", (hdr->crc_ver_init >> 8) & 0xFF);
119 printf("SDRAM init: 0x%04X\n", (hdr->crc_ver_init >> 16) & 0xFFFF);
120 printf("SDRAM config: 0x%04X\n", hdr->config_refresh & 0xFFFF);
121 printf("SDRAM refresh: 0x%04X\n", (hdr->config_refresh >> 16) & 0xFFFF);
122 printf("NCDL values: 0x%08X\n\n", hdr->config_ncdl);
123
124 printf("%i bytes used / %i bytes available (%.2f%%)\n",
125 hdr->len, NVRAM_SPACE - hdr->len,
126 (100.00 / (double)NVRAM_SPACE) * (double)hdr->len);
127
128 return 0;
129 }
130
131
132 int main( int argc, const char *argv[] )
133 {
134 nvram_handle_t *nvram;
135 int commit = 0;
136 int write = 0;
137 int stat = 1;
138 int done = 0;
139 int i;
140
141 /* Ugly... iterate over arguments to see whether we can expect a write */
142 for( i = 1; i < argc; i++ )
143 if( ( !strcmp(argv[i], "set") && ++i < argc ) ||
144 ( !strcmp(argv[i], "unset") && ++i < argc ) ||
145 !strcmp(argv[i], "commit") )
146 {
147 write = 1;
148 break;
149 }
150
151
152 nvram = write ? nvram_open_staging() : nvram_open_rdonly();
153
154 if( nvram != NULL && argc > 1 )
155 {
156 for( i = 1; i < argc; i++ )
157 {
158 if( !strcmp(argv[i], "show") )
159 {
160 stat = do_show(nvram);
161 done++;
162 }
163 else if( !strcmp(argv[i], "info") )
164 {
165 stat = do_info(nvram);
166 done++;
167 }
168 else if( !strcmp(argv[i], "get") || !strcmp(argv[i], "unset") || !strcmp(argv[i], "set") )
169 {
170 if( (i+1) < argc )
171 {
172 switch(argv[i++][0])
173 {
174 case 'g':
175 stat = do_get(nvram, argv[i]);
176 break;
177
178 case 'u':
179 stat = do_unset(nvram, argv[i]);
180 break;
181
182 case 's':
183 stat = do_set(nvram, argv[i]);
184 break;
185 }
186 done++;
187 }
188 else
189 {
190 fprintf(stderr, "Command '%s' requires an argument!\n", argv[i]);
191 done = 0;
192 break;
193 }
194 }
195 else if( !strcmp(argv[i], "commit") )
196 {
197 commit = 1;
198 done++;
199 }
200 else
201 {
202 fprintf(stderr, "Unknown option '%s' !\n", argv[i]);
203 done = 0;
204 break;
205 }
206 }
207
208 if( write )
209 stat = nvram_commit(nvram);
210
211 nvram_close(nvram);
212
213 if( commit )
214 stat = staging_to_nvram();
215 }
216
217 if( !nvram )
218 {
219 fprintf(stderr,
220 "Could not open nvram! Possible reasons are:\n"
221 " - No device found (/proc not mounted or no nvram present)\n"
222 " - Insufficient permissions to open mtd device\n"
223 " - Insufficient memory to complete operation\n"
224 " - Memory mapping failed or not supported\n"
225 );
226
227 stat = 1;
228 }
229 else if( !done )
230 {
231 fprintf(stderr,
232 "Usage:\n"
233 " nvram show\n"
234 " nvram info\n"
235 " nvram get variable\n"
236 " nvram set variable=value [set ...]\n"
237 " nvram unset variable [unset ...]\n"
238 " nvram commit\n"
239 );
240
241 stat = 1;
242 }
243
244 return stat;
245 }
This page took 0.059317 seconds and 5 git commands to generate.