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