2 * Command line interface for libnvram
4 * Copyright 2009, Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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.
21 * The libnvram code is based on Broadcom code for Linux 2.4.x .
28 static nvram_handle_t * nvram_open_rdonly(void)
30 char *file = nvram_find_staging();
33 file = nvram_find_mtd();
36 nvram_handle_t *h = nvram_open(file, NVRAM_RO);
37 if( strcmp(file, NVRAM_STAGING) )
45 static nvram_handle_t * nvram_open_staging(void)
47 if( nvram_find_staging() != NULL || nvram_to_staging() == 0 )
48 return nvram_open(NVRAM_STAGING, NVRAM_RW);
53 static int do_show(nvram_handle_t *nvram)
58 if( (t = nvram_getall(nvram)) != NULL )
62 printf("%s=%s\n", t->name, t->value);
72 static int do_get(nvram_handle_t *nvram, const char *var)
77 if( (val = nvram_get(nvram, var)) != NULL )
86 static int do_unset(nvram_handle_t *nvram, const char *var)
88 return nvram_unset(nvram, var);
91 static int do_set(nvram_handle_t *nvram, const char *pair)
93 char *val = strstr(pair, "=");
94 char var[strlen(pair)];
99 memset(var, 0, sizeof(var));
100 strncpy(var, pair, (int)(val-pair));
101 stat = nvram_set(nvram, var, (char *)(val + 1));
107 static int do_info(nvram_handle_t *nvram)
109 nvram_header_t *hdr = nvram_header(nvram);
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);
116 printf("Magic: 0x%08X\n", hdr->magic);
117 printf("Length: 0x%08X\n", hdr->len);
118 printf("Offset: 0x%08X\n", nvram->offset);
120 printf("CRC8: 0x%02X (calculated: 0x%02X)\n",
121 hdr->crc_ver_init & 0xFF, crc);
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);
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);
136 static void usage(void)
142 " nvram get variable\n"
143 " nvram set variable=value [set ...]\n"
144 " nvram unset variable [unset ...]\n"
149 int main( int argc, const char *argv[] )
151 nvram_handle_t *nvram;
163 /* Ugly... iterate over arguments to see whether we can expect a write */
164 if( ( !strcmp(argv[1], "set") && 2 < argc ) ||
165 ( !strcmp(argv[1], "unset") && 2 < argc ) ||
166 !strcmp(argv[1], "commit") )
170 nvram = write ? nvram_open_staging() : nvram_open_rdonly();
172 if( nvram != NULL && argc > 1 )
174 for( i = 1; i < argc; i++ )
176 if( !strcmp(argv[i], "show") )
178 stat = do_show(nvram);
181 else if( !strcmp(argv[i], "info") )
183 stat = do_info(nvram);
186 else if( !strcmp(argv[i], "get") || !strcmp(argv[i], "unset") || !strcmp(argv[i], "set") )
193 stat = do_get(nvram, argv[i]);
197 stat = do_unset(nvram, argv[i]);
201 stat = do_set(nvram, argv[i]);
208 fprintf(stderr, "Command '%s' requires an argument!\n", argv[i]);
213 else if( !strcmp(argv[i], "commit") )
220 fprintf(stderr, "Unknown option '%s' !\n", argv[i]);
227 stat = nvram_commit(nvram);
232 stat = staging_to_nvram();
238 "Could not open nvram! Possible reasons are:\n"
239 " - No device found (/proc not mounted or no nvram present)\n"
240 " - Insufficient permissions to open mtd device\n"
241 " - Insufficient memory to complete operation\n"
242 " - Memory mapping failed or not supported\n"
243 " - Nvram magic not found in specific nvram partition\n"