cli.c 5.2 KB

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