summaryrefslogtreecommitdiff
path: root/release/src/router/nvram
diff options
context:
space:
mode:
Diffstat (limited to 'release/src/router/nvram')
-rw-r--r--release/src/router/nvram/Makefile37
-rw-r--r--release/src/router/nvram/main.c78
-rw-r--r--release/src/router/nvram/nvram_convert.c92
-rw-r--r--release/src/router/nvram/nvram_convert.h7
-rw-r--r--release/src/router/nvram/nvram_linux.c354
5 files changed, 568 insertions, 0 deletions
diff --git a/release/src/router/nvram/Makefile b/release/src/router/nvram/Makefile
new file mode 100644
index 00000000..438d753c
--- /dev/null
+++ b/release/src/router/nvram/Makefile
@@ -0,0 +1,37 @@
+#
+# Linux NVRAM Makefile
+#
+# Copyright 2005, Broadcom Corporation
+# All Rights Reserved.
+#
+# THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
+# KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
+# SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
+# FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
+#
+# $Id: Makefile,v 1.8 2005/03/07 08:35:32 kanki Exp $
+#
+
+CFLAGS += -I. -I$(SRCBASE)/include -Wall -I$(SRCBASE)/router/shared
+#CFLAGS += -g -DDEBUG
+CFLAGS += -s -O2
+LDFLAGS += -L.
+
+vpath %.c $(SRCBASE)/shared/nvram
+
+all: libnvram.so nvram
+
+install: all
+ install -D libnvram.so $(INSTALLDIR)/usr/lib/libnvram.so
+ $(STRIP) $(INSTALLDIR)/usr/lib/libnvram.so
+ install -D nvram $(INSTALLDIR)/usr/sbin/nvram
+ $(STRIP) $(INSTALLDIR)/usr/sbin/nvram
+
+clean:
+ rm -f *.o *.a *.so nvram
+
+libnvram.so: nvram_linux.o nvram_convert.o
+ $(LD) -shared -o $@ $^
+
+nvram: main.o
+ $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) -lnvram
diff --git a/release/src/router/nvram/main.c b/release/src/router/nvram/main.c
new file mode 100644
index 00000000..c0b43d49
--- /dev/null
+++ b/release/src/router/nvram/main.c
@@ -0,0 +1,78 @@
+/*
+ * Frontend command-line utility for Linux NVRAM layer
+ *
+ * Copyright 2005, Broadcom Corporation
+ * All Rights Reserved.
+ *
+ * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
+ * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
+ * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
+ *
+ * $Id: main.c,v 1.1.1.7 2005/03/07 07:31:19 kanki Exp $
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <typedefs.h>
+#include <bcmnvram.h>
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: nvram [get name] [set name=value] [unset name] [show]\n");
+ exit(0);
+}
+
+/* NVRAM utility */
+int
+main(int argc, char **argv)
+{
+ char *name, *value, buf[NVRAM_SPACE];
+ int size;
+
+ /* Skip program name */
+ --argc;
+ ++argv;
+
+ if (!*argv)
+ usage();
+
+ /* Process the remaining arguments. */
+ for (; *argv; argv++) {
+ if (!strncmp(*argv, "get", 3)) {
+ if (*++argv) {
+ if ((value = nvram_get(*argv)))
+ puts(value);
+ }
+ }
+ else if (!strncmp(*argv, "set", 3)) {
+ if (*++argv) {
+ strncpy(value = buf, *argv, sizeof(buf));
+ name = strsep(&value, "=");
+ nvram_set(name, value);
+ }
+ }
+ else if (!strncmp(*argv, "unset", 5)) {
+ if (*++argv)
+ nvram_unset(*argv);
+ }
+ else if (!strncmp(*argv, "commit", 5)) {
+ nvram_commit();
+ }
+ else if (!strncmp(*argv, "show", 4) ||
+ !strncmp(*argv, "getall", 6)) {
+ nvram_getall(buf, sizeof(buf));
+ for (name = buf; *name; name += strlen(name) + 1)
+ puts(name);
+ size = sizeof(struct nvram_header) + (int) name - (int) buf;
+ fprintf(stderr, "size: %d bytes (%d left)\n", size, NVRAM_SPACE - size);
+ }
+ if (!*argv)
+ break;
+ }
+
+ return 0;
+}
diff --git a/release/src/router/nvram/nvram_convert.c b/release/src/router/nvram/nvram_convert.c
new file mode 100644
index 00000000..3fd3c5e9
--- /dev/null
+++ b/release/src/router/nvram/nvram_convert.c
@@ -0,0 +1,92 @@
+
+#define WL(a) "wl_"a
+#define WL0(a) "wl0_"a
+#define D11G(a) "d11g_"a
+
+#define PPP(a) "ppp_"a
+#define PPPOE(a) "pppoe_"a
+
+struct nvram_convert {
+ char *name; // for WEB
+ char *wl0_name; // for driver
+ char *d11g_name; // for old nv name
+};
+
+struct nvram_convert nvram_converts[] = {
+ // Bellow change from 3.11.48.7
+ { WL("ssid"), WL0("ssid"), ""},
+ { WL("radio"), WL0("mode"), ""},
+ { WL("mode"), WL0("mode"), ""},
+ { WL("wds"), WL0("wds"), ""},
+ { WL("auth"), WL0("auth"), ""},
+ { WL("key"), WL0("key"), ""},
+ { WL("key1"), WL0("key1"), ""},
+ { WL("key2"), WL0("key2"), ""},
+ { WL("key3"), WL0("key3"), ""},
+ { WL("key4"), WL0("key4"), ""},
+ { WL("maclist"), WL0("maclist"), ""},
+ { WL("channel"), WL0("channel"), D11G("channel")},
+ { WL("rateset"), WL0("rateset"), D11G("rateset")},
+ { WL("rts"), WL0("rts"), D11G("rts")},
+ { WL("bcn"), WL0("bcn"), D11G("bcn")},
+ { WL("gmode"), WL0("gmode"), "d11g_mode"},
+ { WL("unit"), WL0("unit"), ""},
+ { WL("ifname"), WL0("ifname"), ""},
+ { WL("phytype"), WL0("phytype"), ""},
+ { WL("country"), WL0("country"), ""},
+ { WL("country_code"), WL0("country_code"), ""},
+ { WL("closed"), WL0("closed"), ""},
+ { WL("lazywds"), WL0("lazywds"), ""},
+ { WL("wep"), WL0("wep"), ""},
+ { WL("macmode"), WL0("macmode"), ""},
+ { WL("rate"), WL0("rate"), D11G("rate")},
+ { WL("frag"), WL0("frag"), D11G("frag")},
+ { WL("dtim"), WL0("dtim"), D11G("dtim")},
+ { WL("plcphdr"), WL0("plcphdr"), ""},
+ { WL("gmode_protection"), WL0("gmode_protection"), ""},
+ { WL("radio"), WL0("radio"), ""},
+ // Below change from 3.21.9.0
+ { WL("auth_mode"), WL0("auth_mode"), ""},
+ { WL("radius_ipaddr"), WL0("radius_ipaddr"), ""},
+ { WL("radius_port"), WL0("radius_port"), ""},
+ { WL("radius_key"), WL0("radius_key"), ""},
+ { WL("wpa_psk"), WL0("wpa_psk"), ""},
+ { WL("wpa_gtk_rekey"), WL0("wpa_gtk_rekey"), ""},
+ { WL("frameburst"), WL0("frameburst"), ""},
+ { WL("crypto"), WL0("crypto"), ""},
+ { WL("ap_isolate"), WL0("ap_isolate"), ""},
+ { WL("afterburner"), WL0("afterburner"), ""},
+ { WL("crypto"), WL0("crypto"), ""},
+ // Below change from 3.63.13.1
+ { WL("akm"), WL0("akm"), ""},
+ { WL("preauth"), WL0("preauth"), ""},
+ { WL("wme"), WL0("wme"), ""},
+ { WL("wme_sta_bk"), WL0("wme_sta_bk"), ""},
+ { WL("wme_sta_be"), WL0("wme_sta_be"), ""},
+ { WL("wme_sta_vi"), WL0("wme_sta_vi"), ""},
+ { WL("wme_sta_vo"), WL0("wme_sta_vo"), ""},
+ { WL("wme_ap_bk"), WL0("wme_ap_bk"), ""},
+ { WL("wme_ap_be"), WL0("wme_ap_be"), ""},
+ { WL("wme_ap_vi"), WL0("wme_ap_vi"), ""},
+ { WL("wme_ap_vo"), WL0("wme_ap_vo"), ""},
+ { WL("wme_no_ack"), WL0("wme_no_ack"), ""},
+ // for PPPoE
+ { PPP("username"), PPPOE("username"), ""},
+ { PPP("passwd"), PPPOE("passwd"), ""},
+ { PPP("idletime"), PPPOE("idletime"), ""},
+ { PPP("keepalive"), PPPOE("keepalive"), ""},
+ { PPP("demand"), PPPOE("demand"), ""},
+ { PPP("service"), PPPOE("service"), ""},
+ { PPP("ac"), PPPOE("ac"), ""},
+ { PPP("static"), PPPOE("static"), ""},
+ { PPP("static_ip"), PPPOE("static_ip"), ""},
+ { PPP("username_1"), PPPOE("username_1"), ""},
+ { PPP("passwd_1"), PPPOE("passwd_1"), ""},
+ { PPP("idletime_1"), PPPOE("idletime_1"), ""},
+ { PPP("keepalive_1"), PPPOE("keepalive_1"), ""},
+ { PPP("demand_1"), PPPOE("demand_1"), ""},
+ { PPP("service_1"), PPPOE("service_1"), ""},
+ { PPP("ac_1"), PPPOE("ac_1"), ""},
+
+ { 0, 0, 0},
+};
diff --git a/release/src/router/nvram/nvram_convert.h b/release/src/router/nvram/nvram_convert.h
new file mode 100644
index 00000000..70e12327
--- /dev/null
+++ b/release/src/router/nvram/nvram_convert.h
@@ -0,0 +1,7 @@
+
+struct nvram_convert {
+ char *name;
+ char *wl0_name;
+ char *d11g_name;
+};
+
diff --git a/release/src/router/nvram/nvram_linux.c b/release/src/router/nvram/nvram_linux.c
new file mode 100644
index 00000000..b91e6746
--- /dev/null
+++ b/release/src/router/nvram/nvram_linux.c
@@ -0,0 +1,354 @@
+/*
+ * NVRAM variable manipulation (Linux user mode half)
+ *
+ * Copyright 2005, Broadcom Corporation
+ * All Rights Reserved.
+ *
+ * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
+ * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
+ * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
+ *
+ * $Id: nvram_linux.c,v 1.18 2005/05/16 12:35:03 honor Exp $
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <error.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
+#include <typedefs.h>
+#include <bcmnvram.h>
+#include <nvram_convert.h>
+#include <shutils.h>
+#include <utils.h>
+
+#define PATH_DEV_NVRAM "/dev/nvram"
+
+/* Globals */
+static int nvram_fd = -1;
+static char *nvram_buf = NULL;
+int check_action(void);
+int file_to_buf(char *path, char *buf, int len);
+
+int
+nvram_init(void *unused)
+{
+ if ((nvram_fd = open(PATH_DEV_NVRAM, O_RDWR)) < 0)
+ goto err;
+
+ /* Map kernel string buffer into user space */
+ if ((nvram_buf = mmap(NULL, NVRAM_SPACE, PROT_READ, MAP_SHARED, nvram_fd, 0)) == MAP_FAILED) {
+ close(nvram_fd);
+ nvram_fd = -1;
+ goto err;
+ }
+
+ return 0;
+
+ err:
+ perror(PATH_DEV_NVRAM);
+ return errno;
+}
+
+char *
+nvram_get(const char *name)
+{
+ size_t count = strlen(name) + 1;
+ char tmp[100], *value;
+ unsigned long *off = (unsigned long *) tmp;
+
+ if (nvram_fd < 0)
+ if (nvram_init(NULL))
+ return NULL;
+
+ if (count > sizeof(tmp)) {
+ if (!(off = malloc(count)))
+ return NULL;
+ }
+
+ /* Get offset into mmap() space */
+ strcpy((char *) off, name);
+
+ count = read(nvram_fd, off, count);
+
+ if (count == sizeof(unsigned long))
+ value = &nvram_buf[*off];
+ else
+ value = NULL;
+
+ if (count < 0)
+ perror(PATH_DEV_NVRAM);
+
+ if (off != (unsigned long *) tmp)
+ free(off);
+
+ return value;
+}
+
+int
+nvram_getall(char *buf, int count)
+{
+ int ret;
+
+ if (nvram_fd < 0)
+ if ((ret = nvram_init(NULL)))
+ return ret;
+
+ if (count == 0)
+ return 0;
+
+ /* Get all variables */
+ *buf = '\0';
+
+ ret = read(nvram_fd, buf, count);
+
+ if (ret < 0)
+ perror(PATH_DEV_NVRAM);
+
+ return (ret == count) ? 0 : ret;
+}
+
+static int
+_nvram_set(const char *name, const char *value)
+{
+ size_t count = strlen(name) + 1;
+ char tmp[100], *buf = tmp;
+ int ret;
+
+ if (nvram_fd < 0)
+ if ((ret = nvram_init(NULL)))
+ return ret;
+
+ /* Unset if value is NULL */
+ if (value)
+ count += strlen(value) + 1;
+
+ if (count > sizeof(tmp)) {
+ if (!(buf = malloc(count)))
+ return -ENOMEM;
+ }
+
+ if (value)
+ sprintf(buf, "%s=%s", name, value);
+ else
+ strcpy(buf, name);
+
+ ret = write(nvram_fd, buf, count);
+
+ if (ret < 0)
+ perror(PATH_DEV_NVRAM);
+
+ if (buf != tmp)
+ free(buf);
+
+ return (ret == count) ? 0 : ret;
+}
+
+int
+nvram_set(const char *name, const char *value)
+{
+ extern struct nvram_convert nvram_converts[];
+ struct nvram_convert *v;
+ int ret;
+
+ ret = _nvram_set(name, value);
+
+ for(v = nvram_converts ; v->name ; v++) {
+ if(!strcmp(v->name, name)){
+ if(strcmp(v->wl0_name,"")) _nvram_set(v->wl0_name, value);
+ if(strcmp(v->d11g_name,"")) _nvram_set(v->d11g_name, value);
+ }
+ }
+
+ return ret;
+}
+
+int
+nvram_unset(const char *name)
+{
+ return _nvram_set(name, NULL);
+}
+
+int
+nvram_commit(void)
+{
+ int ret = 0;
+
+ cprintf("%d nvram_commit(): start\n", getpid());
+
+ if((check_action() == ACT_ERASE_NVRAM)) {
+ int i;
+ cprintf("Waiting 2 seconds...\n");
+ for(i=0;i<2;i++)
+ sleep(1);
+ }
+
+ if((check_action() == ACT_IDLE) ||
+ (check_action() == ACT_SW_RESTORE) ||
+ (check_action() == ACT_HW_RESTORE) ||
+ (check_action() == ACT_NVRAM_COMMIT)) {
+ ACTION("ACT_NVRAM_COMMIT");
+ if (nvram_fd < 0)
+ if ((ret = nvram_init(NULL)))
+ return ret;
+
+ ret = ioctl(nvram_fd, NVRAM_MAGIC, NULL);
+
+ if (ret < 0)
+ perror(PATH_DEV_NVRAM);
+
+ ACTION("ACT_IDLE");
+ cprintf("%d nvram_commit(): end\n", getpid());
+ }
+ else
+ cprintf("%d nvram_commit(): nothing to do...\n", getpid());
+
+ return ret;
+}
+
+int file2nvram(char *filename, char *varname) {
+ FILE *fp;
+ int c,count;
+ int i=0,j=0;
+ char mem[10000],buf[30000];
+
+ if ( !(fp=fopen(filename,"rb") ))
+ return 0;
+
+ count=fread(mem,1,sizeof(mem),fp);
+ fclose(fp);
+ for (j=0;j<count;j++) {
+ if (i > sizeof(buf)-3 )
+ break;
+ c=mem[j];
+ if (c >= 32 && c <= 126 && c != '\\' && c != '~') {
+ buf[i++]=(unsigned char) c;
+ } else if (c==0) {
+ buf[i++]='~';
+ } else {
+ buf[i++]='\\';
+ sprintf(buf+i,"%02X",c);
+ i+=2;
+ }
+ }
+ if (i==0) return 0;
+ buf[i]=0;
+ //fprintf(stderr,"================ > file2nvram %s = [%s] \n",varname,buf);
+ nvram_set(varname,buf);
+ //nvram_commit(); //Barry adds for test
+
+ return 0;
+}
+
+int nvram2file(char *varname, char *filename) {
+ FILE *fp;
+ int c,tmp;
+ int i=0,j=0;
+ char *buf;
+ char mem[10000];
+
+ if ( !(fp=fopen(filename,"wb") ))
+ return 0;
+
+ buf=strdup(nvram_safe_get(varname));
+ //fprintf(stderr,"=================> nvram2file %s = [%s] \n",varname,buf);
+ while ( buf[i] && j < sizeof(mem)-3 ) {
+ if (buf[i] == '\\') {
+ i++;
+ tmp=buf[i+2];
+ buf[i+2]=0;
+ sscanf(buf+i,"%02X",&c);
+ buf[i+2]=tmp;
+ i+=2;
+ mem[j]=c;j++;
+ } else if (buf[i] == '~') {
+ mem[j]=0;j++;
+ i++;
+ } else {
+ mem[j]=buf[i];j++;
+ i++;
+ }
+ }
+ if (j<=0) return j;
+ j=fwrite(mem,1,j,fp);
+ fclose(fp);
+ free(buf);
+ return j;
+}
+
+int
+check_action(void)
+{
+ char buf[80] = "";
+
+ if(file_to_buf(ACTION_FILE, buf, sizeof(buf))){
+ if(!strcmp(buf, "ACT_TFTP_UPGRADE")){
+ cprintf("Upgrading from tftp now ...\n");
+ return ACT_TFTP_UPGRADE;
+ }
+ else if(!strcmp(buf, "ACT_WEBS_UPGRADE")){
+ cprintf("Upgrading from web (https) now ...\n");
+ return ACT_WEBS_UPGRADE;
+ }
+ else if(!strcmp(buf, "ACT_WEB_UPGRADE")){
+ cprintf("Upgrading from web (http) now ...\n");
+ return ACT_WEB_UPGRADE;
+ }
+ else if(!strcmp(buf, "ACT_SW_RESTORE")){
+ cprintf("Receiving restore command from web ...\n");
+ return ACT_SW_RESTORE;
+ }
+ else if(!strcmp(buf, "ACT_HW_RESTORE")){
+ cprintf("Receiving restore commond from resetbutton ...\n");
+ return ACT_HW_RESTORE;
+ }
+ else if(!strcmp(buf, "ACT_NVRAM_COMMIT")){
+ cprintf("Committing nvram now ...\n");
+ return ACT_NVRAM_COMMIT;
+ }
+ else if(!strcmp(buf, "ACT_ERASE_NVRAM")){
+ cprintf("Erasing nvram now ...\n");
+ return ACT_ERASE_NVRAM;
+ }
+ }
+ //fprintf(stderr, "Waiting for upgrading....\n");
+ return ACT_IDLE;
+}
+
+int
+file_to_buf(char *path, char *buf, int len)
+{
+ FILE *fp;
+
+ memset(buf, 0 , len);
+
+ if ((fp = fopen(path, "r"))) {
+ fgets(buf, len, fp);
+ fclose(fp);
+ return 1;
+ }
+
+ return 0;
+}
+
+int
+buf_to_file(char *path, char *buf)
+{
+ FILE *fp;
+
+ if ((fp = fopen(path, "w"))) {
+ fprintf(fp, "%s", buf);
+ fclose(fp);
+ return 1;
+ }
+
+ return 0;
+}