ssize_t nvramdrv_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long buf) #endif { int index, len; const char *p; nvram_ioctl_t *nvr; char *value; switch (cmd) { case NVRAM_IOCTL_GET: nvr = (nvram_ioctl_t __user *)buf; p = nvram_get(nvr->index, nvr->name); if (p == NULL) p = ""; if (copy_to_user(nvr->value, p, strlen(p) + 1)) return -EFAULT; break; case NVRAM_IOCTL_GETALL: nvr = (nvram_ioctl_t __user *)buf; //index = nvr->index; index = 0; len = fb[index].flash_max_len - sizeof(fb[index].env.crc); if (nvram_getall(index, fb[index].env.data) == 0) { if (copy_to_user(nvr->value, fb[index].env.data, len)) return -EFAULT; } break; case NVRAM_IOCTL_SET: nvr = (nvram_ioctl_t *)buf; value = (char *)kmalloc(MAX_VALUE_LEN, GFP_KERNEL); if (!value) return -ENOMEM; if (copy_from_user(value, nvr->value, strlen(nvr->value) + 1)) { kfree(value); return -EFAULT; } nvram_set(nvr->index, nvr->name, value); kfree(value); break; case NVRAM_IOCTL_COMMIT: nvr = (nvram_ioctl_t __user *)buf; nvr->useFile = nvram_commit(nvr->index); break; case NVRAM_IOCTL_CLEAR: nvr = (nvram_ioctl_t __user *)buf; nvram_clear(nvr->index); default: printk(KERN_CRIT "Unsupported ioctl(%d) on nvram\n", cmd); break; } return 0; }
static int nvram_show(int show_all) { char *name, *buf; int buf_len, size; buf_len = NVRAM_SPACE; if (show_all) buf_len *= 2; buf = malloc(buf_len); if (!buf) { perror ("Out of memory!\n"); return -1; } buf[0] = 0; nvram_getall(buf, buf_len, show_all); for (name = buf; *name; name += strlen(name) + 1) puts(name); if (!show_all) { size = sizeof(struct nvram_header) + (int) name - (int) buf; printf("\nsize: %d bytes (%d left)\n", size, NVRAM_SPACE - size); } free(buf); return 0; }
static void getall(char *buffer) { if (nvram_getall(buffer, NVRAM_SPACE) != 0) { fprintf(stderr, "Error reading NVRAM\n"); exit(1); } }
int http_stats(const char *url) { char *buf, *s; char **cur; char *secrets[] = { "os_server", "stats_server", "http_passwd", NULL }; char *files[] = { "/proc/version", "/proc/meminfo", "/proc/cpuinfo", "/proc/interrupts", "/proc/net/dev", "/proc/net/pppoe", "/proc/net/snmp", NULL }; char *contents; if (!(buf = safe_malloc(NVRAMSPACE))) return errno; /* * Get NVRAM variables */ nvram_getall(buf, NVRAMSPACE); for (s = buf; *s; s++) { for (cur = secrets; *cur; cur++) { if (!strncmp(s, *cur, strlen(*cur))) { s += strlen(*cur) + 1; while (*s) *s++ = '*'; break; } } *(s += strlen(s)) = '&'; } /* * Dump interesting /proc entries */ for (cur = files; *cur; cur++) { if ((contents = file2str(*cur))) { s += snprintf(s, buf + BUFSPACE - s, "%s=%s&", *cur, contents); free(contents); } } /* * Report uptime */ s += snprintf(s, buf + BUFSPACE - s, "uptime=%lu&", (unsigned long)time(NULL)); /* * Save */ s += snprintf(s, buf + BUFSPACE - s, "action=save"); buf[BUFSPACE - 1] = '\0'; /* * Post to server */ http_post(url ? : nvram_safe_get("stats_server"), buf, BUFSPACE); free(buf); return 0; }
int ralink_nvram_ioctl(struct inode *inode, struct file *file, unsigned int req, unsigned long arg) #endif { int index, len; const char *p; nvram_ioctl_t *nvr; char *value; switch (req) { case RALINK_NVRAM_IOCTL_GET: nvr = (nvram_ioctl_t __user *)arg; p = nvram_get(nvr->index, nvr->name); if (p == NULL) p = ""; if (copy_to_user(nvr->value, p, strlen(p) + 1)) return -EFAULT; break; case RALINK_NVRAM_IOCTL_GETALL: nvr = (nvram_ioctl_t __user *)arg; index = nvr->index; len = fb[index].flash_max_len - sizeof(fb[index].env.crc); if (nvram_getall(index, fb[index].env.data) == 0) { if (copy_to_user(nvr->value, fb[index].env.data, len)) return -EFAULT; } break; case RALINK_NVRAM_IOCTL_SET: nvr = (nvram_ioctl_t *)arg; value = (char *)kmalloc(MAX_VALUE_LEN, GFP_KERNEL); if (!value) return -ENOMEM; if (copy_from_user(value, nvr->value, strlen(nvr->value) + 1)) { kfree(value); return -EFAULT; } nvram_set(nvr->index, nvr->name, value); kfree(value); break; case RALINK_NVRAM_IOCTL_COMMIT: nvr = (nvram_ioctl_t __user *)arg; nvram_commit(nvr->index); break; case RALINK_NVRAM_IOCTL_CLEAR: nvr = (nvram_ioctl_t __user *)arg; nvram_clear(nvr->index); default: break; } return 0; }
static ssize_t dev_nvram_read(struct file *file, char *buf, size_t count, loff_t *ppos) { char tmp[100], *name = tmp, *value; ssize_t ret; unsigned long off; if ((count+1) > sizeof(tmp)) { if (!(name = kmalloc(count+1, GFP_KERNEL))) return -ENOMEM; } if (copy_from_user(name, buf, count)) { ret = -EFAULT; goto done; } name[count] = '\0'; if (*name == '\0') { /* Get all variables */ ret = nvram_getall(name, count); if (ret == 0) { if (copy_to_user(buf, name, count)) { ret = -EFAULT; goto done; } ret = count; } } else { if (!(value = nvram_get(name))) { ret = 0; goto done; } /* Provide the offset into mmap() space */ off = (unsigned long) value - (unsigned long) nvram_buf; if (put_user(off, (unsigned long *) buf)) { ret = -EFAULT; goto done; } ret = sizeof(unsigned long); } flush_cache_all(); done: if (name != tmp) kfree(name); return ret; }
static int nvram_save(char *file) { FILE *fp; char *name, *buf; unsigned long count, filelen, i; unsigned char rand = 0, temp; if ((fp = fopen(file, "w")) == NULL) return -1; buf = malloc(NVRAM_SPACE); if (!buf) { fclose(fp); perror ("Out of memory!\n"); return -1; } buf[0] = 0; nvram_getall(buf, NVRAM_SPACE, 0); count = 0; for (name = buf; *name; name += strlen(name) + 1) { count = count + strlen(name) + 1; } filelen = count + (1024 - count % 1024); rand = get_rand() % 30; fwrite(PROFILE_HEADER_NEW, 1, 4, fp); fwrite(&filelen, 1, 3, fp); fwrite(&rand, 1, 1, fp); for (i = 0; i < count; i++) { if (buf[i] == 0x0) buf[i] = 0xfd + get_rand() % 3; else buf[i] = 0xff - buf[i] + rand; } fwrite(buf, 1, count, fp); for (i = count; i < filelen; i++) { temp = 0xfd + get_rand() % 3; fwrite(&temp, 1, 1, fp); } fclose(fp); free(buf); return 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_trim_cr(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_trim_cr(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; }
/* * Find variables with <devpath> from flash. 'base' points to the beginning * of the table upon enter and to the end of the table upon exit when success. * Return 0 on success, nonzero on error. */ static int initvars_flash(osl_t *osh, char **base, uint len, char *devpath) { char *vp = *base; char *flash; int err; char *s; uint l, dl, copy_len; /* allocate memory and read in flash */ if (!(flash = MALLOC(osh, NVRAM_SPACE))) return BCME_NOMEM; if ((err = nvram_getall(flash, NVRAM_SPACE))) goto exit; /* grab vars with the <devpath> prefix in name */ dl = strlen(devpath); for (s = flash; s && *s; s += l + 1) { l = strlen(s); /* skip non-matching variable */ if (strncmp(s, devpath, dl)) continue; /* is there enough room to copy? */ copy_len = l - dl + 1; if (len < copy_len) { err = BCME_BUFTOOSHORT; goto exit; } /* no prefix, just the name=value */ strcpy(vp, &s[dl]); vp += copy_len; len -= copy_len; } /* add null string as terminator */ if (len < 1) { err = BCME_BUFTOOSHORT; goto exit; } *vp++ = '\0'; *base = vp; exit: MFREE(osh, flash, NVRAM_SPACE); return err; }
static int do_show(nvram_handle_t *nvram) { nvram_tuple_t *t; int stat = 1; if( (t = nvram_getall(nvram)) != NULL ) { while( t ) { printf("%s=%s\n", t->name, t->value); t = t->next; } stat = 0; } return stat; }
int nvram_show_new(void) { char *name, *buf; int size; buf = malloc(NVRAM_SPACE); if (!buf) { perror ("Out of memory!\n"); return -1; } buf[0] = 0; nvram_getall(buf, NVRAM_SPACE); 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); free(buf); return 0; }
/* NVRAM utility */ int main(int argc, char **argv) { char *name, *value, buf[MAX_NVRAM_SPACE]; char *tmpbuf; //Andy Chiu, 2015/06/09 int size; /* Skip program name */ --argc; ++argv; if (!*argv) usage(); /* Process the arguments */ for (; *argv; ++argv) { if (!strcmp(*argv, "get")) { if (*++argv) { if ((value = nvram_get(*argv))) puts(value); } } else if (!strcmp(*argv, "set")) { if (*++argv) { strncpy(value = buf, *argv, sizeof(buf)); name = strsep(&value, "="); nvram_set(name, value); } } else if (!strcmp(*argv, "unset")) { if (*++argv) nvram_unset(*argv); } else if (!strcmp(*argv, "commit")) { nvram_commit(); } else if (!strcmp(*argv, "save")) { if (*++argv) { nvram_getall(buf, NVRAM_SPACE); nvram_save_new(*argv, buf); } //Andy Chiu, 2015/06/09 }else if(!strncmp(*argv, "fb_save", 7)) { if (*++argv) { tmpbuf = malloc(MAX_NVRAM_SPACE); if(!tmpbuf) { fprintf(stderr, "Can NOT alloc memory!!!"); return 0; } nvram_getall(buf, MAX_NVRAM_SPACE); memcpy(tmpbuf, buf, MAX_NVRAM_SPACE); _secure_conf(tmpbuf); #if 0 FILE *fp = fopen("/tmp/var/fb_conf.test", "w"); if(fp) { fwrite(tmpbuf, 1, MAX_NVRAM_SPACE, fp); fclose(fp); } #endif nvram_save_new(*argv, tmpbuf); free(tmpbuf); } } else if (!strcmp(*argv, "restore")) { if (*++argv) nvram_restore_new(*argv, buf); } else if (!strcmp(*argv, "erase")) { system("nvram_erase"); } else if (!strcmp(*argv, "show") || !strcmp(*argv, "dump")) { nvram_getall(buf, sizeof(buf)); for (name = buf; *name; name += strlen(name) + 1) puts(name); size = sizeof(struct nvram_header) + (int) name - (int) buf; if (**argv != 'd') fprintf(stderr, "size: %d bytes (%d left)\n", size, MAX_NVRAM_SPACE - size); } else usage(); } return 0; }
int ej_show_sysinfo(int eid, webs_t wp, int argc, char_t ** argv) { char *type; char result[2048]; int retval = 0; struct sysinfo sys; char *tmp; strcpy(result,"None"); if (ejArgs(argc, argv, "%s", &type) < 1) { websError(wp, 400, "Insufficient args\n"); return retval; } if (type) { if (strcmp(type,"cpu.model") == 0) { char *buffer = read_whole_file("/proc/cpuinfo"); if (buffer) { tmp = strstr(buffer, "system type"); if (tmp) sscanf(tmp, "system type : %[^\n]", result); free(buffer); } } else if(strcmp(type,"cpu.freq") == 0) { tmp = nvram_get("clkfreq"); if (tmp) sscanf(tmp,"%[^,]s", result); } else if(strcmp(type,"memory.total") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.totalram/(float)MBYTES)); } else if(strcmp(type,"memory.free") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.freeram/(float)MBYTES)); } else if(strcmp(type,"memory.buffer") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.bufferram/(float)MBYTES)); } else if(strcmp(type,"memory.swap.total") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.totalswap/(float)MBYTES)); } else if(strcmp(type,"memory.swap.used") == 0) { sysinfo(&sys); sprintf(result,"%.2f",((sys.totalswap - sys.freeswap) / (float)MBYTES)); } else if(strcmp(type,"cpu.load.1") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.loads[0] / (float)(1<<SI_LOAD_SHIFT))); } else if(strcmp(type,"cpu.load.5") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.loads[1] / (float)(1<<SI_LOAD_SHIFT))); } else if(strcmp(type,"cpu.load.15") == 0) { sysinfo(&sys); sprintf(result,"%.2f",(sys.loads[2] / (float)(1<<SI_LOAD_SHIFT))); } else if(strcmp(type,"nvram.total") == 0) { sprintf(result,"%d",NVRAM_SPACE); } else if(strcmp(type,"nvram.used") == 0) { char *buf; int size = 0; buf = malloc(NVRAM_SPACE); if (buf) { nvram_getall(buf, NVRAM_SPACE); tmp = buf; while (*tmp) tmp += strlen(tmp) +1; size = sizeof(struct nvram_header) + (int) tmp - (int) buf; free(buf); } sprintf(result,"%d",size); } else if(strcmp(type,"jffs.usage") == 0) { struct statvfs fiData; char *mount_info = read_whole_file("/proc/mounts"); if ((mount_info) && (strstr(mount_info, "/jffs")) && (statvfs("/jffs",&fiData) == 0 )) { sprintf(result,"%.2f / %.2f MB",((fiData.f_blocks-fiData.f_bfree) * fiData.f_frsize / (float)MBYTES) ,(fiData.f_blocks * fiData.f_frsize / (float)MBYTES)); } else { strcpy(result,"<i>Unmounted</i>"); } if (mount_info) free(mount_info); } else if(strncmp(type,"temperature",11) == 0) { unsigned int temperature; int radio=0; sscanf(type,"temperature.%d", &radio); temperature = get_phy_temperature(radio); if (temperature == 0) strcpy(result,"<i>disabled</i>"); else sprintf(result,"%u°C", temperature); } else if(strcmp(type,"conn.total") == 0) { FILE* fp; fp = fopen ("/proc/sys/net/ipv4/netfilter/ip_conntrack_count", "r"); if (fp) { if (fgets(result, sizeof(result), fp) != NULL) { fclose(fp); } } } else if(strcmp(type,"conn.active") == 0) { char buf[256]; FILE* fp; unsigned int established = 0; fp = fopen("/proc/net/nf_conntrack", "r"); if (fp) { while (fgets(buf, sizeof(buf), fp) != NULL) { if (strstr(buf,"ESTABLISHED") || ((strstr(buf,"udp")) && (strstr(buf,"ASSURED")))) established++; } fclose(fp); } sprintf(result,"%u",established); } else if(strcmp(type,"conn.max") == 0) { FILE* fp; fp = fopen ("/proc/sys/net/ipv4/netfilter/ip_conntrack_max", "r"); if (fp) { if (fgets(result, sizeof(result), fp) != NULL) { fclose(fp); } } } else if(strncmp(type,"conn.wifi",9) == 0) { int count, radio; char command[10]; sscanf(type,"conn.wifi.%d.%9s", &radio, command); if (strcmp(command,"autho") == 0) { count = get_wifi_clients(radio,SI_WL_QUERY_AUTHO); } else if (strcmp(command,"authe") == 0) { count = get_wifi_clients(radio,SI_WL_QUERY_AUTHE); } else if (strcmp(command,"assoc") == 0) { count = get_wifi_clients(radio,SI_WL_QUERY_ASSOC); } else { count = 0; } if (count == -1) strcpy(result,"<i>off</i>"); else sprintf(result,"%d",count); } else if(strcmp(type,"driver_version") == 0 ) { system("/usr/sbin/wl ver >/tmp/output.txt"); char *buffer = read_whole_file("/tmp/output.txt"); if (buffer) { if (tmp = strstr(buffer, "\n")) strncpy(result, tmp+1, sizeof result); else strncpy(result, buffer, sizeof result); free(buffer); unlink("/tmp/output.txt"); } } else if(strncmp(type,"pid",3) ==0 ) { char service[32]; sscanf(type, "pid.%31s", service); if (strlen(service)) sprintf(result, "%d", pidof(service)); } else if(strncmp(type,"vpnstatus",9) == 0 ) { int num = 0; char service[10], buf[256]; sscanf(type,"vpnstatus.%9[^.].%d", service, &num); if ( strlen(service) && (num > 0) ) { // Trigger OpenVPN to update the status file snprintf(buf, sizeof(buf), "vpn%s%d", service, num); killall(buf, SIGUSR2); // Give it a chance to update the file sleep(1); // Read the status file and repeat it verbatim to the caller sprintf(buf,"/etc/openvpn/%s%d/status", service, num); char *buffer = read_whole_file(buf); if (buffer) { strncpy(result, buffer, sizeof(result)); free(buffer); } } } else { strcpy(result,"Not implemented"); } } retval += websWrite(wp, result); return retval; }
int user_nvram_get(anvram_ioctl_t __user *nvr) { int len, ret; char param[NVRAM_MAX_PARAM_LEN]; char *value; if (!nvr) return -EINVAL; if (nvr->size != sizeof(anvram_ioctl_t)) return -EINVAL; if (nvr->len_value < 1) return -EINVAL; if (nvr->len_param > (NVRAM_MAX_PARAM_LEN-1) || nvr->len_param < 0) return -EINVAL; if (nvr->len_param > 0 && nvr->param) { if (copy_from_user(param, nvr->param, nvr->len_param)) return -EFAULT; param[nvr->len_param] = '\0'; } else { param[0] = '\0'; } ret = 0; if (param[0] == '\0') { if (nvr->len_value < NVRAM_SPACE) { nvr->len_value = NVRAM_SPACE; return -EOVERFLOW; } if (!(value = (char*)kmalloc(NVRAM_SPACE, GFP_KERNEL))) return -ENOMEM; ret = nvram_getall(value, NVRAM_SPACE); if (ret == 0) { nvr->len_value = NVRAM_SPACE; if (copy_to_user(nvr->value, value, NVRAM_SPACE)) ret = -EFAULT; } kfree(value); } else { value = nvram_get(param); if (value) { len = strlen(value) + 1; if (nvr->len_value < len) ret = -EOVERFLOW; else if (copy_to_user(nvr->value, value, len)) ret = -EFAULT; nvr->len_value = len; } else { nvr->len_value = -1; } } return ret; }
int ralink_nvram_ioctl(struct inode *inode, struct file *file, unsigned int req, unsigned long arg) #endif { int index, len; const char *p; nvram_ioctl_t *nvr; char *value; switch (req) { case RALINK_NVRAM_IOCTL_GET: nvr = (nvram_ioctl_t __user *)arg; p = nvram_get(nvr->index, nvr->name); if (p == NULL) p = ""; len = strlen(p) + 1; if (nvr->size < len) { nvr->size = len; return -EOVERFLOW; } if (copy_to_user(nvr->value, p, strlen(p) + 1)) return -EFAULT; break; case RALINK_NVRAM_IOCTL_GETALL: nvr = (nvram_ioctl_t __user *)arg; index = nvr->index; len = fb[index].flash_max_len - sizeof(fb[index].env.crc); if (nvram_getall(index, fb[index].env.data) == 0) { if (copy_to_user(nvr->value, fb[index].env.data, len)) return -EFAULT; } break; case RALINK_NVRAM_IOCTL_SET: nvr = (nvram_ioctl_t *)arg; len = ((nvr->size + MAX_VALUE_LEN) / MAX_VALUE_LEN) * MAX_VALUE_LEN; // Align size+1 bytes to MAX_VALUE_LEN boundary if ((len > MAX_PERMITTED_VALUE_LEN) || (len < 0)) return -EOVERFLOW; value = (char *)kzalloc(len, GFP_KERNEL); if (!value) return -ENOMEM; if (copy_from_user(value, nvr->value, nvr->size)) { KFREE(value); return -EFAULT; } nvram_set(nvr->index, nvr->name, value); KFREE(value); break; case RALINK_NVRAM_IOCTL_COMMIT: nvr = (nvram_ioctl_t __user *)arg; nvram_commit(nvr->index); break; case RALINK_NVRAM_IOCTL_CLEAR: nvr = (nvram_ioctl_t __user *)arg; nvram_clear(nvr->index); default: break; } return 0; }
/* NVRAM utility */ int main(int argc, char **argv) { char *name, *value, buf[MAX_NVRAM_SPACE]; int size; /* Skip program name */ --argc; ++argv; if (!*argv) usage(); /* Process the arguments */ for (; *argv; ++argv) { if (!strcmp(*argv, "get")) { if (*++argv) { if ((value = nvram_get(*argv))) puts(value); } } else if (!strcmp(*argv, "set")) { if (*++argv) { strncpy(value = buf, *argv, sizeof(buf)); name = strsep(&value, "="); nvram_set(name, value); } } else if (!strcmp(*argv, "unset")) { if (*++argv) nvram_unset(*argv); } else if (!strcmp (*argv, "commit") || !strcmp (*argv, "save")) { /*Foxconn add, Jasmine Yang, 03/30/2006 */ nvram_commit(); } else if (!strcmp(*argv, "show") || !strcmp(*argv, "dump")) { nvram_getall(buf, sizeof(buf)); for (name = buf; *name; name += strlen(name) + 1) puts(name); size = sizeof(struct nvram_header) + (int) name - (int) buf; if (**argv != 'd') fprintf(stderr, "size: %d bytes (%d left)\n", size, MAX_NVRAM_SPACE - size); } #ifdef ACOS_MODULES_ENABLE else if (!strncmp (*argv, "loaddefault", 11)) /*Foxconn add, Jasmine Yang, 03/30/2006 */ { /* Write only NVRAM header to flash */ extern int nvram_loaddefault(void); nvram_loaddefault (); } else if (!strncmp (*argv, "version", 7)) /*Foxconn add, Jasmine Yang, 03/30/2006 */ { memset (buf, '\0', sizeof (buf)); if ((value = nvram_safe_get ("pmon_ver"))) { strcpy (buf, "Boot Loader Version : CFE "); strcat (buf, value); strcat (buf, "\n"); } if ((value = nvram_safe_get ("os_version"))) { strcat (buf, "OS Version : Linux "); strcat (buf, value); strcat (buf, "\n"); } puts (buf); } #endif else usage(); } return 0; }
/* NVRAM utility */ int main(int argc, char **argv) { char *name, *value, *buf; int size; char *tmpbuf; //Andy Chiu, 2015/06/09 #ifdef RTCONFIG_CFE_NVRAM_CHK int ret = 0; FILE *fp; #endif /* Skip program name */ --argc; ++argv; if (!*argv) usage(); buf = malloc (MAX_NVRAM_SPACE); if (buf == NULL) { perror ("Out of memory!\n"); return -1; } /* 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, MAX_NVRAM_SPACE); name = strsep(&value, "="); #ifdef RTCONFIG_CFE_NVRAM_CHK ret = nvram_set(name, value); if(ret == 2) { fp = fopen("/var/log/cfecommit_ret", "w"); if(fp!=NULL) { fprintf(fp,"Illegal nvram\n"); fclose(fp); } puts("Illegal nvram format!"); } #else nvram_set(name, value); #endif } } else if (!strncmp(*argv, "unset", 5)) { if (*++argv) nvram_unset(*argv); } else if (!strncmp(*argv, "commit", 5)) { nvram_commit(); } else if (!strncmp(*argv, "save", 4)) { if (*++argv) { nvram_getall(buf, MAX_NVRAM_SPACE); nvram_save_new(*argv, buf); } } //Andy Chiu, 2015/06/09 else if (!strncmp(*argv, "fb_save", 7)) { if (*++argv) { tmpbuf = malloc(MAX_NVRAM_SPACE); if(!tmpbuf) { fprintf(stderr, "Can NOT alloc memory!!!"); return 0; } nvram_getall(buf, MAX_NVRAM_SPACE); memcpy(tmpbuf, buf, MAX_NVRAM_SPACE); _secure_conf(tmpbuf); #if 0 FILE *fp = fopen("/tmp/var/fb_conf.test", "w"); if(fp) { fwrite(tmpbuf, 1, MAX_NVRAM_SPACE, fp); fclose(fp); } #endif nvram_save_new(*argv, tmpbuf); free(tmpbuf); } } else if (!strncmp(*argv, "restore", 7)) { if (*++argv) { nvram_restore_new(*argv, buf); } } else if (!strncmp(*argv, "show", 4) || !strncmp(*argv, "getall", 6)) { nvram_getall(buf, MAX_NVRAM_SPACE); 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, MAX_NVRAM_SPACE - size); } if (!*argv) break; } if (buf != NULL) free (buf); return 0; }