cmd_nvedit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Andreas Heppel <aheppel@sysgo.de>
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /**************************************************************************
  26. *
  27. * Support for persistent environment data
  28. *
  29. * The "environment" is stored as a list of '\0' terminated
  30. * "name=value" strings. The end of the list is marked by a double
  31. * '\0'. New entries are always added at the end. Deleting an entry
  32. * shifts the remaining entries to the front. Replacing an entry is a
  33. * combination of deleting the old value and adding the new one.
  34. *
  35. * The environment is preceeded by a 32 bit CRC over the data part.
  36. *
  37. **************************************************************************
  38. */
  39. #include <common.h>
  40. #include <command.h>
  41. #include <environment.h>
  42. #include <cmd_nvedit.h>
  43. #include <linux/stddef.h>
  44. #include <asm/byteorder.h>
  45. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  46. #include <net.h>
  47. #endif
  48. #if !defined(CFG_ENV_IS_IN_NVRAM) && !defined(CFG_ENV_IS_IN_EEPROM) && !defined(CFG_ENV_IS_IN_FLASH) && !defined(CFG_ENV_IS_NOWHERE)
  49. # error Define one of CFG_ENV_IS_IN_NVRAM, CFG_ENV_IS_IN_EEPROM, CFG_ENV_IS_IN_FLASH, CFG_ENV_IS_NOWHERE
  50. #endif
  51. #define XMK_STR(x) #x
  52. #define MK_STR(x) XMK_STR(x)
  53. /************************************************************************
  54. ************************************************************************/
  55. /* Function that returns a character from the environment */
  56. extern uchar (*env_get_char)(int);
  57. /* Function that returns a pointer to a value from the environment */
  58. /* (Only memory version supported / needed). */
  59. extern uchar *env_get_addr(int);
  60. /* Function that updates CRC of the enironment */
  61. extern void env_crc_update (void);
  62. /************************************************************************
  63. ************************************************************************/
  64. static int envmatch (uchar *, int);
  65. /*
  66. * Table with supported baudrates (defined in config_xyz.h)
  67. */
  68. static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;
  69. #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
  70. /************************************************************************
  71. * Command interface: print one or all environment variables
  72. */
  73. int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  74. {
  75. int i, j, k, nxt;
  76. int rcode = 0;
  77. if (argc == 1) { /* Print all env variables */
  78. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  79. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
  80. ;
  81. for (k=i; k<nxt; ++k)
  82. putc(env_get_char(k));
  83. putc ('\n');
  84. if (ctrlc()) {
  85. puts ("\n ** Abort\n");
  86. return 1;
  87. }
  88. }
  89. printf("\nEnvironment size: %d/%d bytes\n", i, ENV_SIZE);
  90. return 0;
  91. }
  92. for (i=1; i<argc; ++i) { /* print single env variables */
  93. char *name = argv[i];
  94. k = -1;
  95. for (j=0; env_get_char(j) != '\0'; j=nxt+1) {
  96. for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)
  97. ;
  98. k = envmatch(name, j);
  99. if (k < 0) {
  100. continue;
  101. }
  102. puts (name);
  103. putc ('=');
  104. while (k < nxt)
  105. putc(env_get_char(k++));
  106. putc ('\n');
  107. break;
  108. }
  109. if (k < 0) {
  110. printf ("## Error: \"%s\" not defined\n", name);
  111. rcode ++;
  112. }
  113. }
  114. return rcode;
  115. }
  116. /************************************************************************
  117. * Set a new environment variable,
  118. * or replace or delete an existing one.
  119. *
  120. * This function will ONLY work with a in-RAM copy of the environment
  121. */
  122. int _do_setenv (int flag, int argc, char *argv[])
  123. {
  124. DECLARE_GLOBAL_DATA_PTR;
  125. int i, len, oldval;
  126. int console = -1;
  127. uchar *env, *nxt = NULL;
  128. uchar *name;
  129. bd_t *bd = gd->bd;
  130. uchar *env_data = env_get_addr(0);
  131. if (!env_data) /* need copy in RAM */
  132. return 1;
  133. name = argv[1];
  134. /*
  135. * search if variable with this name already exists
  136. */
  137. oldval = -1;
  138. for (env=env_data; *env; env=nxt+1) {
  139. for (nxt=env; *nxt; ++nxt)
  140. ;
  141. if ((oldval = envmatch(name, env-env_data)) >= 0)
  142. break;
  143. }
  144. /*
  145. * Delete any existing definition
  146. */
  147. if (oldval >= 0) {
  148. #ifndef CONFIG_ENV_OVERWRITE
  149. /*
  150. * Ethernet Address and serial# can be set only once
  151. */
  152. if ( (strcmp (name, "serial#") == 0) ||
  153. ((strcmp (name, "ethaddr") == 0)
  154. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  155. && (strcmp (env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
  156. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  157. ) ) {
  158. printf ("Can't overwrite \"%s\"\n", name);
  159. return 1;
  160. }
  161. #endif
  162. /* Check for console redirection */
  163. if (strcmp(name,"stdin") == 0) {
  164. console = stdin;
  165. } else if (strcmp(name,"stdout") == 0) {
  166. console = stdout;
  167. } else if (strcmp(name,"stderr") == 0) {
  168. console = stderr;
  169. }
  170. if (console != -1) {
  171. if (argc < 3) { /* Cannot delete it! */
  172. printf("Can't delete \"%s\"\n", name);
  173. return 1;
  174. }
  175. /* Try assigning specified device */
  176. if (console_assign (console, argv[2]) < 0)
  177. return 1;
  178. }
  179. /*
  180. * Switch to new baudrate if new baudrate is supported
  181. */
  182. if (strcmp(argv[1],"baudrate") == 0) {
  183. int baudrate = simple_strtoul(argv[2], NULL, 10);
  184. int i;
  185. for (i=0; i<N_BAUDRATES; ++i) {
  186. if (baudrate == baudrate_table[i])
  187. break;
  188. }
  189. if (i == N_BAUDRATES) {
  190. printf ("## Baudrate %d bps not supported\n",
  191. baudrate);
  192. return 1;
  193. }
  194. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  195. baudrate);
  196. udelay(50000);
  197. gd->baudrate = baudrate;
  198. serial_setbrg ();
  199. udelay(50000);
  200. for (;;) {
  201. if (getc() == '\r')
  202. break;
  203. }
  204. }
  205. if (*++nxt == '\0') {
  206. if (env > env_data) {
  207. env--;
  208. } else {
  209. *env = '\0';
  210. }
  211. } else {
  212. for (;;) {
  213. *env = *nxt++;
  214. if ((*env == '\0') && (*nxt == '\0'))
  215. break;
  216. ++env;
  217. }
  218. }
  219. *++env = '\0';
  220. }
  221. #ifdef CONFIG_NET_MULTI
  222. if (strncmp(name, "eth", 3) == 0) {
  223. char *end;
  224. int num = simple_strtoul(name+3, &end, 10);
  225. if (strcmp(end, "addr") == 0) {
  226. eth_set_enetaddr(num, argv[2]);
  227. }
  228. }
  229. #endif
  230. /* Delete only ? */
  231. if ((argc < 3) || argv[2] == NULL) {
  232. env_crc_update ();
  233. return 0;
  234. }
  235. /*
  236. * Append new definition at the end
  237. */
  238. for (env=env_data; *env || *(env+1); ++env)
  239. ;
  240. if (env > env_data)
  241. ++env;
  242. /*
  243. * Overflow when:
  244. * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
  245. */
  246. len = strlen(name) + 2;
  247. /* add '=' for first arg, ' ' for all others */
  248. for (i=2; i<argc; ++i) {
  249. len += strlen(argv[i]) + 1;
  250. }
  251. if (len > (&env_data[ENV_SIZE]-env)) {
  252. printf ("## Error: environment overflow, \"%s\" deleted\n", name);
  253. return 1;
  254. }
  255. while ((*env = *name++) != '\0')
  256. env++;
  257. for (i=2; i<argc; ++i) {
  258. char *val = argv[i];
  259. *env = (i==2) ? '=' : ' ';
  260. while ((*++env = *val++) != '\0')
  261. ;
  262. }
  263. /* end is marked with double '\0' */
  264. *++env = '\0';
  265. /* Update CRC */
  266. env_crc_update ();
  267. /*
  268. * Some variables should be updated when the corresponding
  269. * entry in the enviornment is changed
  270. */
  271. if (strcmp(argv[1],"ethaddr") == 0) {
  272. char *s = argv[2]; /* always use only one arg */
  273. char *e;
  274. for (i=0; i<6; ++i) {
  275. bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
  276. if (s) s = (*e) ? e+1 : e;
  277. }
  278. #ifdef CONFIG_NET_MULTI
  279. eth_set_enetaddr(0, argv[2]);
  280. #endif
  281. return 0;
  282. }
  283. if (strcmp(argv[1],"ipaddr") == 0) {
  284. char *s = argv[2]; /* always use only one arg */
  285. char *e;
  286. unsigned long addr;
  287. bd->bi_ip_addr = 0;
  288. for (addr=0, i=0; i<4; ++i) {
  289. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  290. addr <<= 8;
  291. addr |= (val & 0xFF);
  292. if (s) s = (*e) ? e+1 : e;
  293. }
  294. bd->bi_ip_addr = htonl(addr);
  295. return 0;
  296. }
  297. if (strcmp(argv[1],"loadaddr") == 0) {
  298. load_addr = simple_strtoul(argv[2], NULL, 16);
  299. return 0;
  300. }
  301. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  302. if (strcmp(argv[1],"bootfile") == 0) {
  303. copy_filename (BootFile, argv[2], sizeof(BootFile));
  304. return 0;
  305. }
  306. #endif /* CFG_CMD_NET */
  307. return 0;
  308. }
  309. void setenv (char *varname, char *varvalue)
  310. {
  311. char *argv[4] = { "setenv", varname, varvalue, NULL };
  312. _do_setenv (0, 3, argv);
  313. }
  314. int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  315. {
  316. if (argc < 2) {
  317. printf ("Usage:\n%s\n", cmdtp->usage);
  318. return 1;
  319. }
  320. return _do_setenv (flag, argc, argv);
  321. }
  322. /************************************************************************
  323. * Prompt for environment variable
  324. */
  325. #if (CONFIG_COMMANDS & CFG_CMD_ASKENV)
  326. int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  327. {
  328. extern char console_buffer[CFG_CBSIZE];
  329. char message[CFG_CBSIZE];
  330. int size = CFG_CBSIZE - 1;
  331. int len;
  332. char *local_args[4];
  333. local_args[0] = argv[0];
  334. local_args[1] = argv[1];
  335. local_args[2] = NULL;
  336. local_args[3] = NULL;
  337. if (argc < 2) {
  338. printf ("Usage:\n%s\n", cmdtp->usage);
  339. return 1;
  340. }
  341. /* Check the syntax */
  342. switch (argc) {
  343. case 1:
  344. printf ("Usage:\n%s\n", cmdtp->usage);
  345. return 1;
  346. case 2: /* askenv envname */
  347. sprintf (message, "Please enter '%s':", argv[1]);
  348. break;
  349. case 3: /* askenv envname size */
  350. sprintf (message, "Please enter '%s':", argv[1]);
  351. size = simple_strtoul (argv[2], NULL, 10);
  352. break;
  353. default: /* askenv envname message1 ... messagen size */
  354. {
  355. int i;
  356. int pos = 0;
  357. for (i = 2; i < argc - 1; i++) {
  358. if (pos) {
  359. message[pos++] = ' ';
  360. }
  361. strcpy (message+pos, argv[i]);
  362. pos += strlen(argv[i]);
  363. }
  364. message[pos] = '\0';
  365. size = simple_strtoul (argv[argc - 1], NULL, 10);
  366. }
  367. break;
  368. }
  369. if (size >= CFG_CBSIZE)
  370. size = CFG_CBSIZE - 1;
  371. if (size <= 0)
  372. return 1;
  373. /* prompt for input */
  374. len = readline (message);
  375. if (size < len)
  376. console_buffer[size] = '\0';
  377. len = 2;
  378. if (console_buffer[0] != '\0') {
  379. local_args[2] = console_buffer;
  380. len = 3;
  381. }
  382. /* Continue calling setenv code */
  383. return _do_setenv (flag, len, local_args);
  384. }
  385. #endif /* CFG_CMD_ASKENV */
  386. /************************************************************************
  387. * Look up variable from environment,
  388. * return address of storage for that variable,
  389. * or NULL if not found
  390. */
  391. char *getenv (uchar *name)
  392. {
  393. int i, nxt;
  394. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  395. int val;
  396. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  397. if (nxt >= CFG_ENV_SIZE) {
  398. return (NULL);
  399. }
  400. }
  401. if ((val=envmatch(name, i)) < 0)
  402. continue;
  403. return (env_get_addr(val));
  404. }
  405. return (NULL);
  406. }
  407. int getenv_r (uchar *name, uchar *buf, unsigned len)
  408. {
  409. int i, nxt;
  410. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  411. int val, n;
  412. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  413. if (nxt >= CFG_ENV_SIZE) {
  414. return (-1);
  415. }
  416. }
  417. if ((val=envmatch(name, i)) < 0)
  418. continue;
  419. /* found; copy out */
  420. n = 0;
  421. while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
  422. ;
  423. if (len == n)
  424. *buf = '\0';
  425. return (n);
  426. }
  427. return (-1);
  428. }
  429. #if defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) || \
  430. ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == \
  431. (CFG_CMD_ENV|CFG_CMD_FLASH))
  432. int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  433. {
  434. extern char * env_name_spec;
  435. printf ("Saving Environment to %s...\n", env_name_spec);
  436. return (saveenv() ? 1 : 0);
  437. }
  438. #endif
  439. /************************************************************************
  440. * Match a name / name=value pair
  441. *
  442. * s1 is either a simple 'name', or a 'name=value' pair.
  443. * i2 is the environment index for a 'name2=value2' pair.
  444. * If the names match, return the index for the value2, else NULL.
  445. */
  446. static int
  447. envmatch (uchar *s1, int i2)
  448. {
  449. while (*s1 == env_get_char(i2++))
  450. if (*s1++ == '=')
  451. return(i2);
  452. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  453. return(i2);
  454. return(-1);
  455. }