Changeset 4439
- Timestamp:
- 04/25/09 00:38:45 (4 years ago)
- Location:
- luci/trunk/contrib/userspace-nvram
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
luci/trunk/contrib/userspace-nvram/cli.c
r4438 r4439 90 90 int write = 0; 91 91 int stat = 1; 92 int done = 0; 92 93 int i; 93 94 … … 103 104 104 105 105 if( (nvram = write ? nvram_open_staging() : nvram_open_rdonly()) != NULL )106 if( (nvram = write ? nvram_open_staging() : nvram_open_rdonly()) != NULL && argc > 1 ) 106 107 { 107 108 for( i = 1; i < argc; i++ ) … … 110 111 { 111 112 stat = do_show(nvram); 113 done++; 112 114 } 113 115 else if( !strcmp(argv[i], "get") && ++i < argc ) 114 116 { 115 117 stat = do_get(nvram, argv[i]); 118 done++; 116 119 } 117 120 else if( !strcmp(argv[i], "unset") && ++i < argc ) 118 121 { 119 122 stat = do_unset(nvram, argv[i]); 123 done++; 120 124 } 121 125 else if( !strcmp(argv[i], "set") && ++i < argc ) 122 126 { 123 127 stat = do_set(nvram, argv[i]); 128 done++; 124 129 } 125 130 else if( !strcmp(argv[i], "commit") ) 126 131 { 127 132 commit = 1; 133 done++; 128 134 } 129 135 else 130 136 { 131 fprintf(stderr, 132 "Usage:\n" 133 " nvram show\n" 134 " nvram get variable\n" 135 " nvram set variable=value [set ...]\n" 136 " nvram unset variable [unset ...]\n" 137 " nvram commit\n" 138 ); 139 140 return 1; 137 done = 0; 138 break; 141 139 } 142 140 } … … 151 149 } 152 150 151 if( !nvram ) 152 { 153 fprintf(stderr, 154 "Could not open nvram! Possible reasons are:\n" 155 " - No device found (/proc not mounted or no nvram present)\n" 156 " - Insufficient permissions to open mtd device\n" 157 " - Insufficient memory to complete operation\n" 158 " - Memory mapping failed or not supported\n" 159 ); 160 161 stat = 1; 162 } 163 else if( !done ) 164 { 165 fprintf(stderr, 166 "Usage:\n" 167 " nvram show\n" 168 " nvram get variable\n" 169 " nvram set variable=value [set ...]\n" 170 " nvram unset variable [unset ...]\n" 171 " nvram commit\n" 172 ); 173 174 stat = 1; 175 } 176 153 177 return stat; 154 178 } -
luci/trunk/contrib/userspace-nvram/crc.c
r4438 r4439 59 59 60 60 uint8_t hndcrc8 ( 61 uint8_t* pdata, /* pointer to array of data to process */61 const char * pdata, /* pointer to array of data to process */ 62 62 uint32_t nbytes, /* number of input data bytes to process */ 63 63 uint8_t crc /* either CRC8_INIT_VALUE or previous return value */ -
luci/trunk/contrib/userspace-nvram/nvram.c
r4438 r4439 15 15 #include "nvram.h" 16 16 17 #define TRACE() \ 18 printf("%s(%i) in %s()\n", \ 19 __FILE__, __LINE__, __FUNCTION__) 17 #define TRACE(msg) \ 18 printf("%s(%i) in %s(): %s\n", \ 19 __FILE__, __LINE__, __FUNCTION__, msg ? msg : "?") 20 21 size_t nvram_erase_size = 0; 22 20 23 21 24 /* … … 92 95 static int _nvram_rehash(nvram_handle_t *h) 93 96 { 94 nvram_header_t *header = (nvram_header_t *) &h->mmap[NVRAM_S PACE];95 char buf[] = "0xXXXXXXXX", *name, *value, *e nd, *eq;97 nvram_header_t *header = (nvram_header_t *) &h->mmap[NVRAM_START(nvram_erase_size)]; 98 char buf[] = "0xXXXXXXXX", *name, *value, *eq; 96 99 97 100 /* (Re)initialize hash table */ … … 100 103 /* Parse and set "name=value\0 ... \0\0" */ 101 104 name = (char *) &header[1]; 102 /* 103 end = (char *) header + NVRAM_SPACE - 2; 104 end[0] = end[1] = '\0'; 105 */ 105 106 106 for (; *name; name = value + strlen(value) + 1) { 107 107 if (!(eq = strchr(name, '='))) … … 252 252 int nvram_commit(nvram_handle_t *h) 253 253 { 254 nvram_header_t *header = (nvram_header_t *) &h->mmap[NVRAM_S PACE];254 nvram_header_t *header = (nvram_header_t *) &h->mmap[NVRAM_START(nvram_erase_size)]; 255 255 char *init, *config, *refresh, *ncdl; 256 256 char *ptr, *end; … … 281 281 ptr = (char *) header + sizeof(nvram_header_t); 282 282 memset(ptr, 0xFF, NVRAM_SPACE - sizeof(nvram_header_t)); 283 memset(&tmp, 0, sizeof(nvram_header_t)); 283 284 284 285 /* Leave space for a double NUL at the end */ … … 329 330 nvram_header_t *header; 330 331 332 /* If erase size or file are undefined then try to define them */ 333 if( (nvram_erase_size == 0) || (file == NULL) ) 334 { 335 /* Finding the mtd will set the appropriate erase size */ 336 if( file == NULL ) 337 file = nvram_find_mtd(); 338 else 339 (void) nvram_find_mtd(); 340 341 if( nvram_erase_size == 0 ) 342 return NULL; 343 } 344 331 345 if( (fd = open(file, O_RDWR)) > -1 ) 332 346 { 333 347 char *mmap_area = (char *) mmap( 334 NULL, 0x10000, PROT_READ | PROT_WRITE,348 NULL, nvram_erase_size, PROT_READ | PROT_WRITE, 335 349 ( rdonly == NVRAM_RO ) ? MAP_PRIVATE : MAP_SHARED, fd, 0); 336 350 337 351 if( mmap_area != MAP_FAILED ) 338 352 { 339 memset(mmap_area, 0xFF, NVRAM_S PACE);353 memset(mmap_area, 0xFF, NVRAM_START(nvram_erase_size)); 340 354 341 355 if((h = (nvram_handle_t *) malloc(sizeof(nvram_handle_t))) != NULL) … … 345 359 h->fd = fd; 346 360 h->mmap = mmap_area; 347 h->length = 0x10000;348 349 header = (nvram_header_t *) &h->mmap[NVRAM_S PACE];361 h->length = nvram_erase_size; 362 363 header = (nvram_header_t *) &h->mmap[NVRAM_START(nvram_erase_size)]; 350 364 351 365 if( header->magic == NVRAM_MAGIC ) … … 380 394 const char * nvram_find_mtd(void) 381 395 { 382 //return "./samples/nvram.1";383 384 396 FILE *fp; 385 int i ;397 int i, esz; 386 398 char dev[PATH_MAX]; 387 char *path ;388 389 // "/dev/mtdblock/" + ( 0 < x < 99 ) + "ro" +\0 = 19399 char *path = NULL; 400 401 // "/dev/mtdblock/" + ( 0 < x < 99 ) + \0 = 19 390 402 if( (path = (char *) malloc(19)) == NULL ) 391 403 return NULL; … … 393 405 if ((fp = fopen("/proc/mtd", "r"))) { 394 406 while (fgets(dev, sizeof(dev), fp)) { 395 if (sscanf(dev, "mtd%d:", &i) && strstr(dev, "nvram")) { 396 snprintf(path, 19, "/dev/mtdblock/%d", i); 407 if (strstr(dev, "nvram") && sscanf(dev, "mtd%d: %08x", &i, &esz)) { 408 if( (path = (char *) malloc(19)) != NULL ) 409 { 410 nvram_erase_size = esz; 411 snprintf(path, 19, "/dev/mtdblock/%d", i); 412 break; 413 } 397 414 } 398 415 } … … 400 417 } 401 418 402 return (const char *)path;419 return path; 403 420 } 404 421 … … 420 437 { 421 438 int fdmtd, fdstg, stat; 422 const char *mtd ;423 char buf[ 0x10000];439 const char *mtd = nvram_find_mtd(); 440 char buf[nvram_erase_size]; 424 441 425 442 stat = -1; 426 443 427 if( (mtd = nvram_find_mtd()) != NULL)444 if( (mtd != NULL) && (nvram_erase_size > 0) ) 428 445 { 429 446 if( (fdmtd = open(mtd, O_RDONLY)) > -1 ) … … 452 469 { 453 470 int fdmtd, fdstg, stat; 454 const char *mtd ;455 char buf[ 0x10000];471 const char *mtd = nvram_find_mtd(); 472 char buf[nvram_erase_size]; 456 473 457 474 stat = -1; 458 475 459 if( (mtd = nvram_find_mtd()) != NULL)476 if( (mtd != NULL) && (nvram_erase_size > 0) ) 460 477 { 461 478 if( (fdstg = open(NVRAM_STAGING, O_RDONLY)) > -1 ) -
luci/trunk/contrib/userspace-nvram/nvram.h
r4438 r4439 83 83 84 84 /* Computes a crc8 over the input data. */ 85 uint8_t hndcrc8 ( uint8_t* pdata, uint32_t nbytes, uint8_t crc );85 uint8_t hndcrc8 (const char * pdata, uint32_t nbytes, uint8_t crc ); 86 86 87 87 /* Returns the crc value of the nvram. */ … … 114 114 115 115 /* NVRAM constants */ 116 #define NVRAM_SPACE 0x8000 117 #define NVRAM_START(x) x - NVRAM_SPACE 116 118 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */ 117 119 #define NVRAM_CLEAR_MAGIC 0x0 … … 119 121 #define NVRAM_VERSION 1 120 122 #define NVRAM_HEADER_SIZE 20 121 #define NVRAM_SPACE 0x8000122 123 123 124 #define NVRAM_MAX_VALUE_LEN 255
