cmd_nvedit.c 15 KB

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