cmd_nvedit.c 15 KB

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