[package] base-files: support port_state in led config (#6763)
[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 printf("Offset: 0x%08X\n", nvram->offset);
115
116 printf("CRC8: 0x%02X (calculated: 0x%02X)\n",
117 hdr->crc_ver_init & 0xFF, crc);
118
119 printf("Version: 0x%02X\n", (hdr->crc_ver_init >> 8) & 0xFF);
120 printf("SDRAM init: 0x%04X\n", (hdr->crc_ver_init >> 16) & 0xFFFF);
121 printf("SDRAM config: 0x%04X\n", hdr->config_refresh & 0xFFFF);
122 printf("SDRAM refresh: 0x%04X\n", (hdr->config_refresh >> 16) & 0xFFFF);
123 printf("NCDL values: 0x%08X\n\n", hdr->config_ncdl);
124
125 printf("%i bytes used / %i bytes available (%.2f%%)\n",
126 hdr->len, NVRAM_SPACE - hdr->len,
127 (100.00 / (double)NVRAM_SPACE) * (double)hdr->len);
128
129 return 0;
130 }
131
132
133 int main( int argc, const char *argv[] )
134 {
135 nvram_handle_t *nvram;
136 int commit = 0;
137 int write = 0;
138 int stat = 1;
139 int done = 0;
140 int i;
141
142 /* Ugly... iterate over arguments to see whether we can expect a write */
143 for( i = 1; i < argc; i++ )
144 if( ( !strcmp(argv[i], "set") && ++i < argc ) ||
145 ( !strcmp(argv[i], "unset") && ++i < argc ) ||
146 !strcmp(argv[i], "commit") )
147 {
148 write = 1;
149 break;
150 }
151
152
153 nvram = write ? nvram_open_staging() : nvram_open_rdonly();
154
155 if( nvram != NULL && argc > 1 )
156 {
157 for( i = 1; i < argc; i++ )
158 {
159 if( !strcmp(argv[i], "show") )
160 {
161 stat = do_show(nvram);
162 done++;
163 }
164 else if( !strcmp(argv[i], "info") )
165 {
166 stat = do_info(nvram);
167 done++;
168 }
169 else if( !strcmp(argv[i], "get") || !strcmp(argv[i], "unset") || !strcmp(argv[i], "set") )
170 {
171 if( (i+1) < argc )
172 {
173 switch(argv[i++][0])
174 {
175 case 'g':
176 stat = do_get(nvram, argv[i]);
177 break;
178
179 case 'u':
180 stat = do_unset(nvram, argv[i]);
181 break;
182
183 case 's':
184 stat = do_set(nvram, argv[i]);
185 break;
186 }
187 done++;
188 }
189 else
190 {
191 fprintf(stderr, "Command '%s' requires an argument!\n", argv[i]);
192 done = 0;
193 break;
194 }
195 }
196 else if( !strcmp(argv[i], "commit") )
197 {
198 commit = 1;
199 done++;
200 }
201 else
202 {
203 fprintf(stderr, "Unknown option '%s' !\n", argv[i]);
204 done = 0;
205 break;
206 }
207 }
208
209 if( write )
210 stat = nvram_commit(nvram);
211
212 nvram_close(nvram);
213
214 if( commit )
215 stat = staging_to_nvram();
216 }
217
218 if( !nvram )
219 {
220 fprintf(stderr,
221 "Could not open nvram! Possible reasons are:\n"
222 " - No device found (/proc not mounted or no nvram present)\n"
223 " - Insufficient permissions to open mtd device\n"
224 " - Insufficient memory to complete operation\n"
225 " - Memory mapping failed or not supported\n"
226 );
227
228 stat = 1;
229 }
230 else if( !done )
231 {
232 fprintf(stderr,
233 "Usage:\n"
234 " nvram show\n"
235 " nvram info\n"
236 " nvram get variable\n"
237 " nvram set variable=value [set ...]\n"
238 " nvram unset variable [unset ...]\n"
239 " nvram commit\n"
240 );
241
242 stat = 1;
243 }
244
245 return stat;
246 }
This page took 0.068189 seconds and 5 git commands to generate.