cmd_nvedit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 <serial.h>
  44. #include <linux/stddef.h>
  45. #include <asm/byteorder.h>
  46. #if defined(CONFIG_CMD_NET)
  47. #include <net.h>
  48. #endif
  49. DECLARE_GLOBAL_DATA_PTR;
  50. #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
  51. !defined(CONFIG_ENV_IS_IN_FLASH) && \
  52. !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
  53. !defined(CONFIG_ENV_IS_IN_MG_DISK) && \
  54. !defined(CONFIG_ENV_IS_IN_NAND) && \
  55. !defined(CONFIG_ENV_IS_IN_NVRAM) && \
  56. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  57. !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
  58. !defined(CONFIG_ENV_IS_NOWHERE)
  59. # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
  60. SPI_FLASH|MG_DISK|NVRAM|NOWHERE}
  61. #endif
  62. #define XMK_STR(x) #x
  63. #define MK_STR(x) XMK_STR(x)
  64. /************************************************************************
  65. ************************************************************************/
  66. /*
  67. * Table with supported baudrates (defined in config_xyz.h)
  68. */
  69. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  70. #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
  71. /*
  72. * This variable is incremented on each do_setenv (), so it can
  73. * be used via get_env_id() as an indication, if the environment
  74. * has changed or not. So it is possible to reread an environment
  75. * variable only if the environment was changed ... done so for
  76. * example in NetInitLoop()
  77. */
  78. static int env_id = 1;
  79. int get_env_id (void)
  80. {
  81. return env_id;
  82. }
  83. /************************************************************************
  84. * Command interface: print one or all environment variables
  85. */
  86. /*
  87. * state 0: finish printing this string and return (matched!)
  88. * state 1: no matching to be done; print everything
  89. * state 2: continue searching for matched name
  90. */
  91. static int printenv(char *name, int state)
  92. {
  93. int i, j;
  94. char c, buf[17];
  95. i = 0;
  96. buf[16] = '\0';
  97. while (state && env_get_char(i) != '\0') {
  98. if (state == 2 && envmatch((uchar *)name, i) >= 0)
  99. state = 0;
  100. j = 0;
  101. do {
  102. buf[j++] = c = env_get_char(i++);
  103. if (j == sizeof(buf) - 1) {
  104. if (state <= 1)
  105. puts(buf);
  106. j = 0;
  107. }
  108. } while (c != '\0');
  109. if (state <= 1) {
  110. if (j)
  111. puts(buf);
  112. putc('\n');
  113. }
  114. if (ctrlc())
  115. return -1;
  116. }
  117. if (state == 0)
  118. i = 0;
  119. return i;
  120. }
  121. int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  122. {
  123. int i;
  124. int rcode = 0;
  125. if (argc == 1) {
  126. /* print all env vars */
  127. rcode = printenv(NULL, 1);
  128. if (rcode < 0)
  129. return 1;
  130. printf("\nEnvironment size: %d/%ld bytes\n",
  131. rcode, (ulong)ENV_SIZE);
  132. return 0;
  133. }
  134. /* print selected env vars */
  135. for (i = 1; i < argc; ++i) {
  136. char *name = argv[i];
  137. if (printenv(name, 2)) {
  138. printf("## Error: \"%s\" not defined\n", name);
  139. ++rcode;
  140. }
  141. }
  142. return rcode;
  143. }
  144. /************************************************************************
  145. * Set a new environment variable,
  146. * or replace or delete an existing one.
  147. *
  148. * This function will ONLY work with a in-RAM copy of the environment
  149. */
  150. int _do_setenv (int flag, int argc, char *argv[])
  151. {
  152. int i, len, oldval;
  153. int console = -1;
  154. uchar *env, *nxt = NULL;
  155. char *name;
  156. bd_t *bd = gd->bd;
  157. uchar *env_data = env_get_addr(0);
  158. if (!env_data) /* need copy in RAM */
  159. return 1;
  160. name = argv[1];
  161. if (strchr(name, '=')) {
  162. printf ("## Error: illegal character '=' in variable name \"%s\"\n", name);
  163. return 1;
  164. }
  165. env_id++;
  166. /*
  167. * search if variable with this name already exists
  168. */
  169. oldval = -1;
  170. for (env=env_data; *env; env=nxt+1) {
  171. for (nxt=env; *nxt; ++nxt)
  172. ;
  173. if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
  174. break;
  175. }
  176. /*
  177. * Delete any existing definition
  178. */
  179. if (oldval >= 0) {
  180. #ifndef CONFIG_ENV_OVERWRITE
  181. /*
  182. * Ethernet Address and serial# can be set only once,
  183. * ver is readonly.
  184. */
  185. if (
  186. #ifdef CONFIG_HAS_UID
  187. /* Allow serial# forced overwrite with 0xdeaf4add flag */
  188. ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) ||
  189. #else
  190. (strcmp (name, "serial#") == 0) ||
  191. #endif
  192. ((strcmp (name, "ethaddr") == 0)
  193. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  194. && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)
  195. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  196. ) ) {
  197. printf ("Can't overwrite \"%s\"\n", name);
  198. return 1;
  199. }
  200. #endif
  201. /* Check for console redirection */
  202. if (strcmp(name,"stdin") == 0) {
  203. console = stdin;
  204. } else if (strcmp(name,"stdout") == 0) {
  205. console = stdout;
  206. } else if (strcmp(name,"stderr") == 0) {
  207. console = stderr;
  208. }
  209. if (console != -1) {
  210. if (argc < 3) { /* Cannot delete it! */
  211. printf("Can't delete \"%s\"\n", name);
  212. return 1;
  213. }
  214. #ifdef CONFIG_CONSOLE_MUX
  215. i = iomux_doenv(console, argv[2]);
  216. if (i)
  217. return i;
  218. #else
  219. /* Try assigning specified device */
  220. if (console_assign (console, argv[2]) < 0)
  221. return 1;
  222. #ifdef CONFIG_SERIAL_MULTI
  223. if (serial_assign (argv[2]) < 0)
  224. return 1;
  225. #endif
  226. #endif /* CONFIG_CONSOLE_MUX */
  227. }
  228. /*
  229. * Switch to new baudrate if new baudrate is supported
  230. */
  231. if (strcmp(argv[1],"baudrate") == 0) {
  232. int baudrate = simple_strtoul(argv[2], NULL, 10);
  233. int i;
  234. for (i=0; i<N_BAUDRATES; ++i) {
  235. if (baudrate == baudrate_table[i])
  236. break;
  237. }
  238. if (i == N_BAUDRATES) {
  239. printf ("## Baudrate %d bps not supported\n",
  240. baudrate);
  241. return 1;
  242. }
  243. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  244. baudrate);
  245. udelay(50000);
  246. gd->baudrate = baudrate;
  247. #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
  248. gd->bd->bi_baudrate = baudrate;
  249. #endif
  250. serial_setbrg ();
  251. udelay(50000);
  252. for (;;) {
  253. if (getc() == '\r')
  254. break;
  255. }
  256. }
  257. if (*++nxt == '\0') {
  258. if (env > env_data) {
  259. env--;
  260. } else {
  261. *env = '\0';
  262. }
  263. } else {
  264. for (;;) {
  265. *env = *nxt++;
  266. if ((*env == '\0') && (*nxt == '\0'))
  267. break;
  268. ++env;
  269. }
  270. }
  271. *++env = '\0';
  272. }
  273. /* Delete only ? */
  274. if ((argc < 3) || argv[2] == NULL) {
  275. env_crc_update ();
  276. return 0;
  277. }
  278. /*
  279. * Append new definition at the end
  280. */
  281. for (env=env_data; *env || *(env+1); ++env)
  282. ;
  283. if (env > env_data)
  284. ++env;
  285. /*
  286. * Overflow when:
  287. * "name" + "=" + "val" +"\0\0" > ENV_SIZE - (env-env_data)
  288. */
  289. len = strlen(name) + 2;
  290. /* add '=' for first arg, ' ' for all others */
  291. for (i=2; i<argc; ++i) {
  292. len += strlen(argv[i]) + 1;
  293. }
  294. if (len > (&env_data[ENV_SIZE]-env)) {
  295. printf ("## Error: environment overflow, \"%s\" deleted\n", name);
  296. return 1;
  297. }
  298. while ((*env = *name++) != '\0')
  299. env++;
  300. for (i=2; i<argc; ++i) {
  301. char *val = argv[i];
  302. *env = (i==2) ? '=' : ' ';
  303. while ((*++env = *val++) != '\0')
  304. ;
  305. }
  306. /* end is marked with double '\0' */
  307. *++env = '\0';
  308. /* Update CRC */
  309. env_crc_update ();
  310. /*
  311. * Some variables should be updated when the corresponding
  312. * entry in the enviornment is changed
  313. */
  314. if (strcmp(argv[1],"ethaddr") == 0)
  315. return 0;
  316. if (strcmp(argv[1],"ipaddr") == 0) {
  317. char *s = argv[2]; /* always use only one arg */
  318. char *e;
  319. unsigned long addr;
  320. bd->bi_ip_addr = 0;
  321. for (addr=0, i=0; i<4; ++i) {
  322. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  323. addr <<= 8;
  324. addr |= (val & 0xFF);
  325. if (s) s = (*e) ? e+1 : e;
  326. }
  327. bd->bi_ip_addr = htonl(addr);
  328. return 0;
  329. }
  330. if (strcmp(argv[1],"loadaddr") == 0) {
  331. load_addr = simple_strtoul(argv[2], NULL, 16);
  332. return 0;
  333. }
  334. #if defined(CONFIG_CMD_NET)
  335. if (strcmp(argv[1],"bootfile") == 0) {
  336. copy_filename (BootFile, argv[2], sizeof(BootFile));
  337. return 0;
  338. }
  339. #endif
  340. #ifdef CONFIG_AMIGAONEG3SE
  341. if (strcmp(argv[1], "vga_fg_color") == 0 ||
  342. strcmp(argv[1], "vga_bg_color") == 0 ) {
  343. extern void video_set_color(unsigned char attr);
  344. extern unsigned char video_get_attr(void);
  345. video_set_color(video_get_attr());
  346. return 0;
  347. }
  348. #endif /* CONFIG_AMIGAONEG3SE */
  349. return 0;
  350. }
  351. int setenv (char *varname, char *varvalue)
  352. {
  353. char *argv[4] = { "setenv", varname, varvalue, NULL };
  354. if (varvalue == NULL)
  355. return _do_setenv (0, 2, argv);
  356. else
  357. return _do_setenv (0, 3, argv);
  358. }
  359. #ifdef CONFIG_HAS_UID
  360. void forceenv (char *varname, char *varvalue)
  361. {
  362. char *argv[4] = { "forceenv", varname, varvalue, NULL };
  363. _do_setenv (0xdeaf4add, 3, argv);
  364. }
  365. #endif
  366. int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  367. {
  368. if (argc < 2) {
  369. cmd_usage(cmdtp);
  370. return 1;
  371. }
  372. return _do_setenv (flag, argc, argv);
  373. }
  374. /************************************************************************
  375. * Prompt for environment variable
  376. */
  377. #if defined(CONFIG_CMD_ASKENV)
  378. int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  379. {
  380. extern char console_buffer[CONFIG_SYS_CBSIZE];
  381. char message[CONFIG_SYS_CBSIZE];
  382. int size = CONFIG_SYS_CBSIZE - 1;
  383. int len;
  384. char *local_args[4];
  385. local_args[0] = argv[0];
  386. local_args[1] = argv[1];
  387. local_args[2] = NULL;
  388. local_args[3] = NULL;
  389. if (argc < 2) {
  390. cmd_usage(cmdtp);
  391. return 1;
  392. }
  393. /* Check the syntax */
  394. switch (argc) {
  395. case 1:
  396. cmd_usage(cmdtp);
  397. return 1;
  398. case 2: /* askenv envname */
  399. sprintf (message, "Please enter '%s':", argv[1]);
  400. break;
  401. case 3: /* askenv envname size */
  402. sprintf (message, "Please enter '%s':", argv[1]);
  403. size = simple_strtoul (argv[2], NULL, 10);
  404. break;
  405. default: /* askenv envname message1 ... messagen size */
  406. {
  407. int i;
  408. int pos = 0;
  409. for (i = 2; i < argc - 1; i++) {
  410. if (pos) {
  411. message[pos++] = ' ';
  412. }
  413. strcpy (message+pos, argv[i]);
  414. pos += strlen(argv[i]);
  415. }
  416. message[pos] = '\0';
  417. size = simple_strtoul (argv[argc - 1], NULL, 10);
  418. }
  419. break;
  420. }
  421. if (size >= CONFIG_SYS_CBSIZE)
  422. size = CONFIG_SYS_CBSIZE - 1;
  423. if (size <= 0)
  424. return 1;
  425. /* prompt for input */
  426. len = readline (message);
  427. if (size < len)
  428. console_buffer[size] = '\0';
  429. len = 2;
  430. if (console_buffer[0] != '\0') {
  431. local_args[2] = console_buffer;
  432. len = 3;
  433. }
  434. /* Continue calling setenv code */
  435. return _do_setenv (flag, len, local_args);
  436. }
  437. #endif
  438. /************************************************************************
  439. * Look up variable from environment,
  440. * return address of storage for that variable,
  441. * or NULL if not found
  442. */
  443. char *getenv (char *name)
  444. {
  445. int i, nxt;
  446. WATCHDOG_RESET();
  447. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  448. int val;
  449. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  450. if (nxt >= CONFIG_ENV_SIZE) {
  451. return (NULL);
  452. }
  453. }
  454. if ((val=envmatch((uchar *)name, i)) < 0)
  455. continue;
  456. return ((char *)env_get_addr(val));
  457. }
  458. return (NULL);
  459. }
  460. int getenv_r (char *name, char *buf, unsigned len)
  461. {
  462. int i, nxt;
  463. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  464. int val, n;
  465. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  466. if (nxt >= CONFIG_ENV_SIZE) {
  467. return (-1);
  468. }
  469. }
  470. if ((val=envmatch((uchar *)name, i)) < 0)
  471. continue;
  472. /* found; copy out */
  473. n = 0;
  474. while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
  475. ;
  476. if (len == n)
  477. *buf = '\0';
  478. return (n);
  479. }
  480. return (-1);
  481. }
  482. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  483. int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  484. {
  485. extern char * env_name_spec;
  486. printf ("Saving Environment to %s...\n", env_name_spec);
  487. return (saveenv() ? 1 : 0);
  488. }
  489. U_BOOT_CMD(
  490. saveenv, 1, 0, do_saveenv,
  491. "save environment variables to persistent storage",
  492. ""
  493. );
  494. #endif
  495. /************************************************************************
  496. * Match a name / name=value pair
  497. *
  498. * s1 is either a simple 'name', or a 'name=value' pair.
  499. * i2 is the environment index for a 'name2=value2' pair.
  500. * If the names match, return the index for the value2, else NULL.
  501. */
  502. int envmatch (uchar *s1, int i2)
  503. {
  504. while (*s1 == env_get_char(i2++))
  505. if (*s1++ == '=')
  506. return(i2);
  507. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  508. return(i2);
  509. return(-1);
  510. }
  511. /**************************************************/
  512. U_BOOT_CMD(
  513. printenv, CONFIG_SYS_MAXARGS, 1, do_printenv,
  514. "print environment variables",
  515. "\n - print values of all environment variables\n"
  516. "printenv name ...\n"
  517. " - print value of environment variable 'name'"
  518. );
  519. U_BOOT_CMD(
  520. setenv, CONFIG_SYS_MAXARGS, 0, do_setenv,
  521. "set environment variables",
  522. "name value ...\n"
  523. " - set environment variable 'name' to 'value ...'\n"
  524. "setenv name\n"
  525. " - delete environment variable 'name'"
  526. );
  527. #if defined(CONFIG_CMD_ASKENV)
  528. U_BOOT_CMD(
  529. askenv, CONFIG_SYS_MAXARGS, 1, do_askenv,
  530. "get environment variables from stdin",
  531. "name [message] [size]\n"
  532. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  533. "askenv name\n"
  534. " - get environment variable 'name' from stdin\n"
  535. "askenv name size\n"
  536. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  537. "askenv name [message] size\n"
  538. " - display 'message' string and get environment variable 'name'"
  539. "from stdin (max 'size' chars)"
  540. );
  541. #endif
  542. #if defined(CONFIG_CMD_RUN)
  543. int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  544. U_BOOT_CMD(
  545. run, CONFIG_SYS_MAXARGS, 1, do_run,
  546. "run commands in an environment variable",
  547. "var [...]\n"
  548. " - run the commands in the environment variable(s) 'var'"
  549. );
  550. #endif