X-Git-Url: http://git.rohieb.name/openwrt.git/blobdiff_plain/fad131f967cbb1ba415fca80e90361e5e9ed7a48..e194f60a49979616867b44d249156e37728b3cbd:/package/nvram/src/nvram.c?ds=sidebyside diff --git a/package/nvram/src/nvram.c b/package/nvram/src/nvram.c index 78baa9431..a0bc006d1 100644 --- a/package/nvram/src/nvram.c +++ b/package/nvram/src/nvram.c @@ -2,7 +2,7 @@ * NVRAM variable manipulation (common) * * Copyright 2004, Broadcom Corporation - * Copyright 2009, OpenWrt.org + * Copyright 2009-2010, OpenWrt.org * All Rights Reserved. * * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY @@ -142,7 +142,7 @@ static int _nvram_rehash(nvram_handle_t *h) /* Get nvram header. */ nvram_header_t * nvram_header(nvram_handle_t *h) { - return (nvram_header_t *) &h->mmap[NVRAM_START(nvram_erase_size)]; + return (nvram_header_t *) &h->mmap[h->offset]; } /* Get the value of an NVRAM variable. */ @@ -337,10 +337,12 @@ int nvram_commit(nvram_handle_t *h) /* Open NVRAM and obtain a handle. */ nvram_handle_t * nvram_open(const char *file, int rdonly) { + int i; int fd; char *mtd = NULL; nvram_handle_t *h; nvram_header_t *header; + int offset = -1; /* If erase size or file are undefined then try to define them */ if( (nvram_erase_size == 0) || (file == NULL) ) @@ -357,19 +359,32 @@ nvram_handle_t * nvram_open(const char *file, int rdonly) { char *mmap_area = (char *) mmap( NULL, nvram_erase_size, PROT_READ | PROT_WRITE, - ( rdonly == NVRAM_RO ) ? MAP_PRIVATE : MAP_SHARED, fd, 0); + (( rdonly == NVRAM_RO ) ? MAP_PRIVATE : MAP_SHARED) | MAP_LOCKED, fd, 0); if( mmap_area != MAP_FAILED ) { - memset(mmap_area, 0xFF, NVRAM_START(nvram_erase_size)); + for( i = 0; i <= ((nvram_erase_size - NVRAM_SPACE) / sizeof(uint32_t)); i++ ) + { + if( ((uint32_t *)mmap_area)[i] == NVRAM_MAGIC ) + { + offset = i * sizeof(uint32_t); + break; + } + } - if((h = (nvram_handle_t *) malloc(sizeof(nvram_handle_t))) != NULL) + if( offset < 0 ) + { + free(mtd); + return NULL; + } + else if( (h = malloc(sizeof(nvram_handle_t))) != NULL ) { memset(h, 0, sizeof(nvram_handle_t)); h->fd = fd; h->mmap = mmap_area; h->length = nvram_erase_size; + h->offset = offset; header = nvram_header(h); @@ -411,8 +426,18 @@ char * nvram_find_mtd(void) char dev[PATH_MAX]; char *path = NULL; struct stat s; + int supported = 1; + + /* Refuse any operation on the WGT634U */ + if( (fp = fopen("/proc/diag/model", "r")) ) + { + if( fgets(dev, sizeof(dev), fp) && !strncmp(dev, "Netgear WGT634U", 15) ) + supported = 0; + + fclose(fp); + } - if( (fp = fopen("/proc/mtd", "r")) ) + if( supported && (fp = fopen("/proc/mtd", "r")) ) { while( fgets(dev, sizeof(dev), fp) ) {