cmd_nvedit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. return 0;
  344. }
  345. int setenv (char *varname, char *varvalue)
  346. {
  347. char *argv[4] = { "setenv", varname, varvalue, NULL };
  348. if ((varvalue == NULL) || (varvalue[0] == '\0'))
  349. return _do_setenv (0, 2, argv);
  350. else
  351. return _do_setenv (0, 3, argv);
  352. }
  353. #ifdef CONFIG_HAS_UID
  354. void forceenv (char *varname, char *varvalue)
  355. {
  356. char *argv[4] = { "forceenv", varname, varvalue, NULL };
  357. _do_setenv (0xdeaf4add, 3, argv);
  358. }
  359. #endif
  360. int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  361. {
  362. if (argc < 2) {
  363. cmd_usage(cmdtp);
  364. return 1;
  365. }
  366. return _do_setenv (flag, argc, argv);
  367. }
  368. /************************************************************************
  369. * Prompt for environment variable
  370. */
  371. #if defined(CONFIG_CMD_ASKENV)
  372. int do_askenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  373. {
  374. extern char console_buffer[CONFIG_SYS_CBSIZE];
  375. char message[CONFIG_SYS_CBSIZE];
  376. int size = CONFIG_SYS_CBSIZE - 1;
  377. int len;
  378. char *local_args[4];
  379. local_args[0] = argv[0];
  380. local_args[1] = argv[1];
  381. local_args[2] = NULL;
  382. local_args[3] = NULL;
  383. if (argc < 2) {
  384. cmd_usage(cmdtp);
  385. return 1;
  386. }
  387. /* Check the syntax */
  388. switch (argc) {
  389. case 1:
  390. cmd_usage(cmdtp);
  391. return 1;
  392. case 2: /* askenv envname */
  393. sprintf (message, "Please enter '%s':", argv[1]);
  394. break;
  395. case 3: /* askenv envname size */
  396. sprintf (message, "Please enter '%s':", argv[1]);
  397. size = simple_strtoul (argv[2], NULL, 10);
  398. break;
  399. default: /* askenv envname message1 ... messagen size */
  400. {
  401. int i;
  402. int pos = 0;
  403. for (i = 2; i < argc - 1; i++) {
  404. if (pos) {
  405. message[pos++] = ' ';
  406. }
  407. strcpy (message+pos, argv[i]);
  408. pos += strlen(argv[i]);
  409. }
  410. message[pos] = '\0';
  411. size = simple_strtoul (argv[argc - 1], NULL, 10);
  412. }
  413. break;
  414. }
  415. if (size >= CONFIG_SYS_CBSIZE)
  416. size = CONFIG_SYS_CBSIZE - 1;
  417. if (size <= 0)
  418. return 1;
  419. /* prompt for input */
  420. len = readline (message);
  421. if (size < len)
  422. console_buffer[size] = '\0';
  423. len = 2;
  424. if (console_buffer[0] != '\0') {
  425. local_args[2] = console_buffer;
  426. len = 3;
  427. }
  428. /* Continue calling setenv code */
  429. return _do_setenv (flag, len, local_args);
  430. }
  431. #endif
  432. /************************************************************************
  433. * Interactively edit an environment variable
  434. */
  435. #if defined(CONFIG_CMD_EDITENV)
  436. int do_editenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  437. {
  438. char buffer[CONFIG_SYS_CBSIZE];
  439. char *init_val;
  440. int len;
  441. if (argc < 2) {
  442. cmd_usage(cmdtp);
  443. return 1;
  444. }
  445. /* Set read buffer to initial value or empty sting */
  446. init_val = getenv(argv[1]);
  447. if (init_val)
  448. len = sprintf(buffer, "%s", init_val);
  449. else
  450. buffer[0] = '\0';
  451. readline_into_buffer("edit: ", buffer);
  452. return setenv(argv[1], buffer);
  453. }
  454. #endif /* CONFIG_CMD_EDITENV */
  455. /************************************************************************
  456. * Look up variable from environment,
  457. * return address of storage for that variable,
  458. * or NULL if not found
  459. */
  460. char *getenv (char *name)
  461. {
  462. int i, nxt;
  463. WATCHDOG_RESET();
  464. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  465. int val;
  466. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  467. if (nxt >= CONFIG_ENV_SIZE) {
  468. return (NULL);
  469. }
  470. }
  471. if ((val=envmatch((uchar *)name, i)) < 0)
  472. continue;
  473. return ((char *)env_get_addr(val));
  474. }
  475. return (NULL);
  476. }
  477. int getenv_r (char *name, char *buf, unsigned len)
  478. {
  479. int i, nxt;
  480. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  481. int val, n;
  482. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  483. if (nxt >= CONFIG_ENV_SIZE) {
  484. return (-1);
  485. }
  486. }
  487. if ((val=envmatch((uchar *)name, i)) < 0)
  488. continue;
  489. /* found; copy out */
  490. n = 0;
  491. while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
  492. ;
  493. if (len == n)
  494. *buf = '\0';
  495. return (n);
  496. }
  497. return (-1);
  498. }
  499. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  500. int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  501. {
  502. extern char * env_name_spec;
  503. printf ("Saving Environment to %s...\n", env_name_spec);
  504. return (saveenv() ? 1 : 0);
  505. }
  506. U_BOOT_CMD(
  507. saveenv, 1, 0, do_saveenv,
  508. "save environment variables to persistent storage",
  509. ""
  510. );
  511. #endif
  512. /************************************************************************
  513. * Match a name / name=value pair
  514. *
  515. * s1 is either a simple 'name', or a 'name=value' pair.
  516. * i2 is the environment index for a 'name2=value2' pair.
  517. * If the names match, return the index for the value2, else NULL.
  518. */
  519. int envmatch (uchar *s1, int i2)
  520. {
  521. while (*s1 == env_get_char(i2++))
  522. if (*s1++ == '=')
  523. return(i2);
  524. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  525. return(i2);
  526. return(-1);
  527. }
  528. /**************************************************/
  529. #if defined(CONFIG_CMD_EDITENV)
  530. U_BOOT_CMD(
  531. editenv, 2, 0, do_editenv,
  532. "edit environment variable",
  533. "name\n"
  534. " - edit environment variable 'name'"
  535. );
  536. #endif
  537. U_BOOT_CMD(
  538. printenv, CONFIG_SYS_MAXARGS, 1, do_printenv,
  539. "print environment variables",
  540. "\n - print values of all environment variables\n"
  541. "printenv name ...\n"
  542. " - print value of environment variable 'name'"
  543. );
  544. U_BOOT_CMD(
  545. setenv, CONFIG_SYS_MAXARGS, 0, do_setenv,
  546. "set environment variables",
  547. "name value ...\n"
  548. " - set environment variable 'name' to 'value ...'\n"
  549. "setenv name\n"
  550. " - delete environment variable 'name'"
  551. );
  552. #if defined(CONFIG_CMD_ASKENV)
  553. U_BOOT_CMD(
  554. askenv, CONFIG_SYS_MAXARGS, 1, do_askenv,
  555. "get environment variables from stdin",
  556. "name [message] [size]\n"
  557. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  558. "askenv name\n"
  559. " - get environment variable 'name' from stdin\n"
  560. "askenv name size\n"
  561. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  562. "askenv name [message] size\n"
  563. " - display 'message' string and get environment variable 'name'"
  564. "from stdin (max 'size' chars)"
  565. );
  566. #endif
  567. #if defined(CONFIG_CMD_RUN)
  568. int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  569. U_BOOT_CMD(
  570. run, CONFIG_SYS_MAXARGS, 1, do_run,
  571. "run commands in an environment variable",
  572. "var [...]\n"
  573. " - run the commands in the environment variable(s) 'var'"
  574. );
  575. #endif