cli.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Command line interface for libnvram
  3. *
  4. * Copyright 2009, Jo-Philipp Wich <[email protected]>
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. *
  20. *
  21. * The libnvram code is based on Broadcom code for Linux 2.4.x .
  22. *
  23. */
  24. #include "nvram.h"
  25. static nvram_handle_t * nvram_open_rdonly(void)
  26. {
  27. char *file = nvram_find_staging();
  28. if( file == NULL )
  29. file = nvram_find_mtd();
  30. if( file != NULL ) {
  31. nvram_handle_t *h = nvram_open(file, NVRAM_RO);
  32. if( strcmp(file, NVRAM_STAGING) )
  33. free(file);
  34. return h;
  35. }
  36. return NULL;
  37. }
  38. static nvram_handle_t * nvram_open_staging(void)
  39. {
  40. if( nvram_find_staging() != NULL || nvram_to_staging() == 0 )
  41. return nvram_open(NVRAM_STAGING, NVRAM_RW);
  42. return NULL;
  43. }
  44. static int do_show(nvram_handle_t *nvram)
  45. {
  46. nvram_tuple_t *t;
  47. int stat = 1;
  48. if( (t = nvram_getall(nvram)) != NULL )
  49. {
  50. while( t )
  51. {
  52. printf("%s=%s\n", t->name, t->value);
  53. t = t->next;
  54. }
  55. stat = 0;
  56. }
  57. return stat;
  58. }
  59. static int do_get(nvram_handle_t *nvram, const char *var)
  60. {
  61. const char *val;
  62. int stat = 1;
  63. if( (val = nvram_get(nvram, var)) != NULL )
  64. {
  65. printf("%s\n", val);
  66. stat = 0;
  67. }
  68. return stat;
  69. }
  70. static int do_unset(nvram_handle_t *nvram, const char *var)
  71. {
  72. return nvram_unset(nvram, var);
  73. }
  74. static int do_set(nvram_handle_t *nvram, const char *pair)
  75. {
  76. char *val = strstr(pair, "=");
  77. char var[strlen(pair)];
  78. int stat = 1;
  79. if( val != NULL )
  80. {
  81. memset(var, 0, sizeof(var));
  82. strncpy(var, pair, (int)(val-pair));
  83. stat = nvram_set(nvram, var, (char *)(val + 1));
  84. }
  85. return stat;
  86. }
  87. static int do_info(nvram_handle_t *nvram)
  88. {
  89. nvram_header_t *hdr = nvram_header(nvram);
  90. /* CRC8 over the last 11 bytes of the header and data bytes */
  91. uint8_t crc = hndcrc8((unsigned char *) &hdr[0] + NVRAM_CRC_START_POSITION,
  92. hdr->len - NVRAM_CRC_START_POSITION, 0xff);
  93. /* Show info */
  94. printf("Magic: 0x%08X\n", hdr->magic);
  95. printf("Length: 0x%08X\n", hdr->len);
  96. printf("Offset: 0x%08X\n", nvram->offset);
  97. printf("CRC8: 0x%02X (calculated: 0x%02X)\n",
  98. hdr->crc_ver_init & 0xFF, crc);
  99. printf("Version: 0x%02X\n", (hdr->crc_ver_init >> 8) & 0xFF);
  100. printf("SDRAM init: 0x%04X\n", (hdr->crc_ver_init >> 16) & 0xFFFF);
  101. printf("SDRAM config: 0x%04X\n", hdr->config_refresh & 0xFFFF);
  102. printf("SDRAM refresh: 0x%04X\n", (hdr->config_refresh >> 16) & 0xFFFF);
  103. printf("NCDL values: 0x%08X\n\n", hdr->config_ncdl);
  104. printf("%i bytes used / %i bytes available (%.2f%%)\n",
  105. hdr->len, nvram->length - nvram->offset - hdr->len,
  106. (100.00 / (double)(nvram->length - nvram->offset)) * (double)hdr->len);
  107. return 0;
  108. }
  109. static void usage(void)
  110. {
  111. fprintf(stderr,
  112. "Usage:\n"
  113. " nvram show\n"
  114. " nvram info\n"
  115. " nvram get variable\n"
  116. " nvram set variable=value [set ...]\n"
  117. " nvram unset variable [unset ...]\n"
  118. " nvram commit\n"
  119. );
  120. }
  121. int main( int argc, const char *argv[] )
  122. {
  123. nvram_handle_t *nvram;
  124. int commit = 0;
  125. int write = 0;
  126. int stat = 1;
  127. int done = 0;
  128. int i;
  129. if( argc < 2 ) {
  130. usage();
  131. return 1;
  132. }
  133. /* Ugly... iterate over arguments to see whether we can expect a write */
  134. if( ( !strcmp(argv[1], "set") && 2 < argc ) ||
  135. ( !strcmp(argv[1], "unset") && 2 < argc ) ||
  136. !strcmp(argv[1], "commit") )
  137. write = 1;
  138. nvram = write ? nvram_open_staging() : nvram_open_rdonly();
  139. if( nvram != NULL && argc > 1 )
  140. {
  141. for( i = 1; i < argc; i++ )
  142. {
  143. if( !strcmp(argv[i], "show") )
  144. {
  145. stat = do_show(nvram);
  146. done++;
  147. }
  148. else if( !strcmp(argv[i], "info") )
  149. {
  150. stat = do_info(nvram);
  151. done++;
  152. }
  153. else if( !strcmp(argv[i], "get") || !strcmp(argv[i], "unset") || !strcmp(argv[i], "set") )
  154. {
  155. if( (i+1) < argc )
  156. {
  157. switch(argv[i++][0])
  158. {
  159. case 'g':
  160. stat = do_get(nvram, argv[i]);
  161. break;
  162. case 'u':
  163. stat = do_unset(nvram, argv[i]);
  164. break;
  165. case 's':
  166. stat = do_set(nvram, argv[i]);
  167. break;
  168. }
  169. done++;
  170. }
  171. else
  172. {
  173. fprintf(stderr, "Command '%s' requires an argument!\n", argv[i]);
  174. done = 0;
  175. break;
  176. }
  177. }
  178. else if( !strcmp(argv[i], "commit") )
  179. {
  180. commit = 1;
  181. done++;
  182. }
  183. else
  184. {
  185. fprintf(stderr, "Unknown option '%s' !\n", argv[i]);
  186. done = 0;
  187. break;
  188. }
  189. }
  190. if( write )
  191. stat = nvram_commit(nvram);
  192. nvram_close(nvram);
  193. if( commit )
  194. stat = staging_to_nvram();
  195. }
  196. if( !nvram )
  197. {
  198. fprintf(stderr,
  199. "Could not open nvram! Possible reasons are:\n"
  200. " - No device found (/proc not mounted or no nvram present)\n"
  201. " - Insufficient permissions to open mtd device\n"
  202. " - Insufficient memory to complete operation\n"
  203. " - Memory mapping failed or not supported\n"
  204. " - Nvram magic not found in specific nvram partition\n"
  205. );
  206. stat = 1;
  207. }
  208. else if( !done )
  209. {
  210. usage();
  211. stat = 1;
  212. }
  213. return stat;
  214. }