cmd_nvedit.c 12 KB

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