cli.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 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("CRC8: 0x%02X (calculated: 0x%02X)\n",
  93. hdr->crc_ver_init & 0xFF, crc);
  94. printf("Version: 0x%02X\n", (hdr->crc_ver_init >> 8) & 0xFF);
  95. printf("SDRAM init: 0x%04X\n", (hdr->crc_ver_init >> 16) & 0xFFFF);
  96. printf("SDRAM config: 0x%04X\n", hdr->config_refresh & 0xFFFF);
  97. printf("SDRAM refresh: 0x%04X\n", (hdr->config_refresh >> 16) & 0xFFFF);
  98. printf("NCDL values: 0x%08X\n\n", hdr->config_ncdl);
  99. printf("%i bytes used / %i bytes available (%.2f%%)\n",
  100. hdr->len, NVRAM_SPACE - hdr->len,
  101. (100.00 / (double)NVRAM_SPACE) * (double)hdr->len);
  102. return 0;
  103. }
  104. int main( int argc, const char *argv[] )
  105. {
  106. nvram_handle_t *nvram;
  107. int commit = 0;
  108. int write = 0;
  109. int stat = 1;
  110. int done = 0;
  111. int i;
  112. /* Ugly... iterate over arguments to see whether we can expect a write */
  113. for( i = 1; i < argc; i++ )
  114. if( ( !strcmp(argv[i], "set") && ++i < argc ) ||
  115. ( !strcmp(argv[i], "unset") && ++i < argc ) ||
  116. !strcmp(argv[i], "commit") )
  117. {
  118. write = 1;
  119. break;
  120. }
  121. nvram = write ? nvram_open_staging() : nvram_open_rdonly();
  122. if( nvram != NULL && argc > 1 )
  123. {
  124. for( i = 1; i < argc; i++ )
  125. {
  126. if( !strcmp(argv[i], "show") )
  127. {
  128. stat = do_show(nvram);
  129. done++;
  130. }
  131. else if( !strcmp(argv[i], "info") )
  132. {
  133. stat = do_info(nvram);
  134. done++;
  135. }
  136. else if( !strcmp(argv[i], "get") && ++i < argc )
  137. {
  138. stat = do_get(nvram, argv[i]);
  139. done++;
  140. }
  141. else if( !strcmp(argv[i], "unset") && ++i < argc )
  142. {
  143. stat = do_unset(nvram, argv[i]);
  144. done++;
  145. }
  146. else if( !strcmp(argv[i], "set") && ++i < argc )
  147. {
  148. stat = do_set(nvram, argv[i]);
  149. done++;
  150. }
  151. else if( !strcmp(argv[i], "commit") )
  152. {
  153. commit = 1;
  154. done++;
  155. }
  156. else
  157. {
  158. fprintf(stderr, "Unknown option '%s' !\n", argv[i]);
  159. done = 0;
  160. break;
  161. }
  162. }
  163. if( write )
  164. stat = nvram_commit(nvram);
  165. nvram_close(nvram);
  166. if( commit )
  167. stat = staging_to_nvram();
  168. }
  169. if( !nvram )
  170. {
  171. fprintf(stderr,
  172. "Could not open nvram! Possible reasons are:\n"
  173. " - No device found (/proc not mounted or no nvram present)\n"
  174. " - Insufficient permissions to open mtd device\n"
  175. " - Insufficient memory to complete operation\n"
  176. " - Memory mapping failed or not supported\n"
  177. );
  178. stat = 1;
  179. }
  180. else if( !done )
  181. {
  182. fprintf(stderr,
  183. "Usage:\n"
  184. " nvram show\n"
  185. " nvram info\n"
  186. " nvram get variable\n"
  187. " nvram set variable=value [set ...]\n"
  188. " nvram unset variable [unset ...]\n"
  189. " nvram commit\n"
  190. );
  191. stat = 1;
  192. }
  193. return stat;
  194. }