cmd_nvedit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * (C) Copyright 2000-2010
  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. * Support for persistent environment data
  27. *
  28. * The "environment" is stored on external storage as a list of '\0'
  29. * terminated "name=value" strings. The end of the list is marked by
  30. * a double '\0'. The environment is preceeded by a 32 bit CRC over
  31. * the data part and, in case of redundant environment, a byte of
  32. * flags.
  33. *
  34. * This linearized representation will also be used before
  35. * relocation, i. e. as long as we don't have a full C runtime
  36. * environment. After that, we use a hash table.
  37. */
  38. #include <common.h>
  39. #include <command.h>
  40. #include <environment.h>
  41. #include <search.h>
  42. #include <errno.h>
  43. #include <malloc.h>
  44. #include <watchdog.h>
  45. #include <serial.h>
  46. #include <linux/stddef.h>
  47. #include <asm/byteorder.h>
  48. #if defined(CONFIG_CMD_NET)
  49. #include <net.h>
  50. #endif
  51. DECLARE_GLOBAL_DATA_PTR;
  52. #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
  53. !defined(CONFIG_ENV_IS_IN_FLASH) && \
  54. !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
  55. !defined(CONFIG_ENV_IS_IN_MG_DISK) && \
  56. !defined(CONFIG_ENV_IS_IN_MMC) && \
  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|MMC|NOWHERE}
  64. #endif
  65. #define XMK_STR(x) #x
  66. #define MK_STR(x) XMK_STR(x)
  67. /*
  68. * Maximum expected input data size for import command
  69. */
  70. #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
  71. ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
  72. /*
  73. * Table with supported baudrates (defined in config_xyz.h)
  74. */
  75. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  76. #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
  77. /*
  78. * This variable is incremented on each do_env_set(), so it can
  79. * be used via get_env_id() as an indication, if the environment
  80. * has changed or not. So it is possible to reread an environment
  81. * variable only if the environment was changed ... done so for
  82. * example in NetInitLoop()
  83. */
  84. static int env_id = 1;
  85. int get_env_id (void)
  86. {
  87. return env_id;
  88. }
  89. /*
  90. * Command interface: print one or all environment variables
  91. *
  92. * Returns 0 in case of error, or length of printed string
  93. */
  94. static int env_print(char *name)
  95. {
  96. char *res = NULL;
  97. size_t len;
  98. if (name) { /* print a single name */
  99. ENTRY e, *ep;
  100. e.key = name;
  101. e.data = NULL;
  102. hsearch_r(e, FIND, &ep, &env_htab);
  103. if (ep == NULL)
  104. return 0;
  105. len = printf ("%s=%s\n", ep->key, ep->data);
  106. return len;
  107. }
  108. /* print whole list */
  109. len = hexport_r(&env_htab, '\n', &res, 0);
  110. if (len > 0) {
  111. puts(res);
  112. free(res);
  113. return len;
  114. }
  115. /* should never happen */
  116. return 0;
  117. }
  118. int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  119. {
  120. int i;
  121. int rcode = 0;
  122. if (argc == 1) {
  123. /* print all env vars */
  124. rcode = env_print(NULL);
  125. if (!rcode)
  126. return 1;
  127. printf("\nEnvironment size: %d/%ld bytes\n",
  128. rcode, (ulong)ENV_SIZE);
  129. return 0;
  130. }
  131. /* print selected env vars */
  132. for (i = 1; i < argc; ++i) {
  133. int rc = env_print(argv[i]);
  134. if (!rc) {
  135. printf("## Error: \"%s\" not defined\n", argv[i]);
  136. ++rcode;
  137. }
  138. }
  139. return rcode;
  140. }
  141. /*
  142. * Set a new environment variable,
  143. * or replace or delete an existing one.
  144. */
  145. int _do_env_set (int flag, int argc, char * const argv[])
  146. {
  147. bd_t *bd = gd->bd;
  148. int i, len;
  149. int console = -1;
  150. char *name, *value, *s;
  151. ENTRY e, *ep;
  152. name = argv[1];
  153. if (strchr(name, '=')) {
  154. printf ("## Error: illegal character '=' in variable name \"%s\"\n", name);
  155. return 1;
  156. }
  157. env_id++;
  158. /*
  159. * search if variable with this name already exists
  160. */
  161. e.key = name;
  162. e.data = NULL;
  163. hsearch_r(e, FIND, &ep, &env_htab);
  164. /* Check for console redirection */
  165. if (strcmp(name,"stdin") == 0) {
  166. console = stdin;
  167. } else if (strcmp(name,"stdout") == 0) {
  168. console = stdout;
  169. } else if (strcmp(name,"stderr") == 0) {
  170. console = stderr;
  171. }
  172. if (console != -1) {
  173. if (argc < 3) { /* Cannot delete it! */
  174. printf("Can't delete \"%s\"\n", name);
  175. return 1;
  176. }
  177. #ifdef CONFIG_CONSOLE_MUX
  178. i = iomux_doenv(console, argv[2]);
  179. if (i)
  180. return i;
  181. #else
  182. /* Try assigning specified device */
  183. if (console_assign (console, argv[2]) < 0)
  184. return 1;
  185. #ifdef CONFIG_SERIAL_MULTI
  186. if (serial_assign (argv[2]) < 0)
  187. return 1;
  188. #endif
  189. #endif /* CONFIG_CONSOLE_MUX */
  190. }
  191. /*
  192. * Some variables like "ethaddr" and "serial#" can be set only
  193. * once and cannot be deleted; also, "ver" is readonly.
  194. */
  195. if (ep) { /* variable exists */
  196. #ifndef CONFIG_ENV_OVERWRITE
  197. if ((strcmp (name, "serial#") == 0) ||
  198. ((strcmp (name, "ethaddr") == 0)
  199. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  200. && (strcmp (ep->data,MK_STR(CONFIG_ETHADDR)) != 0)
  201. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  202. ) ) {
  203. printf ("Can't overwrite \"%s\"\n", name);
  204. return 1;
  205. }
  206. #endif
  207. /*
  208. * Switch to new baudrate if new baudrate is supported
  209. */
  210. if (strcmp(name,"baudrate") == 0) {
  211. int baudrate = simple_strtoul(argv[2], NULL, 10);
  212. int i;
  213. for (i=0; i<N_BAUDRATES; ++i) {
  214. if (baudrate == baudrate_table[i])
  215. break;
  216. }
  217. if (i == N_BAUDRATES) {
  218. printf ("## Baudrate %d bps not supported\n",
  219. baudrate);
  220. return 1;
  221. }
  222. printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  223. baudrate);
  224. udelay(50000);
  225. gd->baudrate = baudrate;
  226. #if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
  227. gd->bd->bi_baudrate = baudrate;
  228. #endif
  229. serial_setbrg ();
  230. udelay(50000);
  231. for (;;) {
  232. if (getc() == '\r')
  233. break;
  234. }
  235. }
  236. }
  237. /* Delete only ? */
  238. if ((argc < 3) || argv[2] == NULL) {
  239. int rc = hdelete_r(name, &env_htab);
  240. return !rc;
  241. }
  242. /*
  243. * Insert / replace new value
  244. */
  245. for (i=2,len=0; i<argc; ++i) {
  246. len += strlen(argv[i]) + 1;
  247. }
  248. if ((value = malloc(len)) == NULL) {
  249. printf("## Can't malloc %d bytes\n", len);
  250. return 1;
  251. }
  252. for (i=2,s=value; i<argc; ++i) {
  253. char *v = argv[i];
  254. while ((*s++ = *v++) != '\0')
  255. ;
  256. *(s-1) = ' ';
  257. }
  258. if (s != value)
  259. *--s = '\0';
  260. e.key = name;
  261. e.data = value;
  262. hsearch_r(e, ENTER, &ep, &env_htab);
  263. free(value);
  264. if (!ep) {
  265. printf("## Error inserting \"%s\" variable, errno=%d\n",
  266. name, errno);
  267. return 1;
  268. }
  269. /*
  270. * Some variables should be updated when the corresponding
  271. * entry in the environment is changed
  272. */
  273. if (strcmp(name,"ipaddr") == 0) {
  274. char *s = argv[2]; /* always use only one arg */
  275. char *e;
  276. unsigned long addr;
  277. bd->bi_ip_addr = 0;
  278. for (addr=0, i=0; i<4; ++i) {
  279. ulong val = s ? simple_strtoul(s, &e, 10) : 0;
  280. addr <<= 8;
  281. addr |= (val & 0xFF);
  282. if (s) s = (*e) ? e+1 : e;
  283. }
  284. bd->bi_ip_addr = htonl(addr);
  285. return 0;
  286. } else if (strcmp(argv[1],"loadaddr") == 0) {
  287. load_addr = simple_strtoul(argv[2], NULL, 16);
  288. return 0;
  289. }
  290. #if defined(CONFIG_CMD_NET)
  291. else if (strcmp(argv[1],"bootfile") == 0) {
  292. copy_filename (BootFile, argv[2], sizeof(BootFile));
  293. return 0;
  294. }
  295. #endif
  296. return 0;
  297. }
  298. int setenv (char *varname, char *varvalue)
  299. {
  300. char * const argv[4] = { "setenv", varname, varvalue, NULL };
  301. if ((varvalue == NULL) || (varvalue[0] == '\0'))
  302. return _do_env_set(0, 2, argv);
  303. else
  304. return _do_env_set(0, 3, argv);
  305. }
  306. int do_env_set (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  307. {
  308. if (argc < 2)
  309. return cmd_usage(cmdtp);
  310. return _do_env_set(flag, argc, argv);
  311. }
  312. /*
  313. * Prompt for environment variable
  314. */
  315. #if defined(CONFIG_CMD_ASKENV)
  316. int do_env_ask ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  317. {
  318. extern char console_buffer[CONFIG_SYS_CBSIZE];
  319. char message[CONFIG_SYS_CBSIZE];
  320. int size = CONFIG_SYS_CBSIZE - 1;
  321. int i, len, pos;
  322. char *local_args[4];
  323. local_args[0] = argv[0];
  324. local_args[1] = argv[1];
  325. local_args[2] = NULL;
  326. local_args[3] = NULL;
  327. /* Check the syntax */
  328. switch (argc) {
  329. case 1:
  330. return cmd_usage(cmdtp);
  331. case 2: /* env_ask envname */
  332. sprintf(message, "Please enter '%s':", argv[1]);
  333. break;
  334. case 3: /* env_ask envname size */
  335. sprintf(message, "Please enter '%s':", argv[1]);
  336. size = simple_strtoul(argv[2], NULL, 10);
  337. break;
  338. default: /* env_ask envname message1 ... messagen size */
  339. for (i=2,pos=0; i < argc - 1; i++) {
  340. if (pos) {
  341. message[pos++] = ' ';
  342. }
  343. strcpy(message+pos, argv[i]);
  344. pos += strlen(argv[i]);
  345. }
  346. message[pos] = '\0';
  347. size = simple_strtoul(argv[argc - 1], NULL, 10);
  348. break;
  349. }
  350. if (size >= CONFIG_SYS_CBSIZE)
  351. size = CONFIG_SYS_CBSIZE - 1;
  352. if (size <= 0)
  353. return 1;
  354. /* prompt for input */
  355. len = readline(message);
  356. if (size < len)
  357. console_buffer[size] = '\0';
  358. len = 2;
  359. if (console_buffer[0] != '\0') {
  360. local_args[2] = console_buffer;
  361. len = 3;
  362. }
  363. /* Continue calling setenv code */
  364. return _do_env_set(flag, len, local_args);
  365. }
  366. #endif
  367. /*
  368. * Interactively edit an environment variable
  369. */
  370. #if defined(CONFIG_CMD_EDITENV)
  371. int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  372. {
  373. char buffer[CONFIG_SYS_CBSIZE];
  374. char *init_val;
  375. int len;
  376. if (argc < 2)
  377. return cmd_usage(cmdtp);
  378. /* Set read buffer to initial value or empty sting */
  379. init_val = getenv(argv[1]);
  380. if (init_val)
  381. len = sprintf(buffer, "%s", init_val);
  382. else
  383. buffer[0] = '\0';
  384. readline_into_buffer("edit: ", buffer);
  385. return setenv(argv[1], buffer);
  386. }
  387. #endif /* CONFIG_CMD_EDITENV */
  388. /*
  389. * Look up variable from environment,
  390. * return address of storage for that variable,
  391. * or NULL if not found
  392. */
  393. char *getenv (char *name)
  394. {
  395. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  396. ENTRY e, *ep;
  397. WATCHDOG_RESET();
  398. e.key = name;
  399. e.data = NULL;
  400. hsearch_r(e, FIND, &ep, &env_htab);
  401. return (ep ? ep->data : NULL);
  402. }
  403. /* restricted capabilities before import */
  404. if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
  405. return (char *)(gd->env_buf);
  406. return NULL;
  407. }
  408. /*
  409. * Look up variable from environment for restricted C runtime env.
  410. */
  411. int getenv_f (char *name, char *buf, unsigned len)
  412. {
  413. int i, nxt;
  414. for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
  415. int val, n;
  416. for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
  417. if (nxt >= CONFIG_ENV_SIZE) {
  418. return (-1);
  419. }
  420. }
  421. if ((val=envmatch((uchar *)name, i)) < 0)
  422. continue;
  423. /* found; copy out */
  424. for (n=0; n<len; ++n, ++buf) {
  425. if ((*buf = env_get_char(val++)) == '\0')
  426. return n;
  427. }
  428. if (n)
  429. *--buf = '\0';
  430. printf("env_buf too small [%d]\n", len);
  431. return n;
  432. }
  433. return (-1);
  434. }
  435. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  436. int do_env_save (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  437. {
  438. extern char * env_name_spec;
  439. printf ("Saving Environment to %s...\n", env_name_spec);
  440. return (saveenv() ? 1 : 0);
  441. }
  442. U_BOOT_CMD(
  443. saveenv, 1, 0, do_env_save,
  444. "save environment variables to persistent storage",
  445. ""
  446. );
  447. #endif
  448. /*
  449. * Match a name / name=value pair
  450. *
  451. * s1 is either a simple 'name', or a 'name=value' pair.
  452. * i2 is the environment index for a 'name2=value2' pair.
  453. * If the names match, return the index for the value2, else NULL.
  454. */
  455. int envmatch (uchar *s1, int i2)
  456. {
  457. while (*s1 == env_get_char(i2++))
  458. if (*s1++ == '=')
  459. return(i2);
  460. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  461. return(i2);
  462. return(-1);
  463. }
  464. static int do_env_default(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  465. {
  466. if ((argc != 2) || (strcmp(argv[1], "-f") != 0)) {
  467. return cmd_usage(cmdtp);
  468. }
  469. set_default_env("## Resetting to default environment\n");
  470. return 0;
  471. }
  472. static int do_env_delete(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  473. {
  474. printf("Not implemented yet\n");
  475. return 0;
  476. }
  477. #ifdef CONFIG_CMD_EXPORTENV
  478. /*
  479. * env export [-t | -b | -c] addr [size]
  480. * -t: export as text format; if size is given, data will be
  481. * padded with '\0' bytes; if not, one terminating '\0'
  482. * will be added (which is included in the "filesize"
  483. * setting so you can for exmple copy this to flash and
  484. * keep the termination).
  485. * -b: export as binary format (name=value pairs separated by
  486. * '\0', list end marked by double "\0\0")
  487. * -c: export as checksum protected environment format as
  488. * used for example by "saveenv" command
  489. * addr: memory address where environment gets stored
  490. * size: size of output buffer
  491. *
  492. * With "-c" and size is NOT given, then the export command will
  493. * format the data as currently used for the persistent storage,
  494. * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
  495. * prepend a valid CRC32 checksum and, in case of resundant
  496. * environment, a "current" redundancy flag. If size is given, this
  497. * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
  498. * checksum and redundancy flag will be inserted.
  499. *
  500. * With "-b" and "-t", always only the real data (including a
  501. * terminating '\0' byte) will be written; here the optional size
  502. * argument will be used to make sure not to overflow the user
  503. * provided buffer; the command will abort if the size is not
  504. * sufficient. Any remainign space will be '\0' padded.
  505. *
  506. * On successful return, the variable "filesize" will be set.
  507. * Note that filesize includes the trailing/terminating '\0' byte(s).
  508. *
  509. * Usage szenario: create a text snapshot/backup of the current settings:
  510. *
  511. * => env export -t 100000
  512. * => era ${backup_addr} +${filesize}
  513. * => cp.b 100000 ${backup_addr} ${filesize}
  514. *
  515. * Re-import this snapshot, deleting all other settings:
  516. *
  517. * => env import -d -t ${backup_addr}
  518. */
  519. static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  520. {
  521. char buf[32];
  522. char *addr, *cmd, *res;
  523. size_t size;
  524. ssize_t len;
  525. env_t *envp;
  526. char sep = '\n';
  527. int chk = 0;
  528. int fmt = 0;
  529. cmd = *argv;
  530. while (--argc > 0 && **++argv == '-') {
  531. char *arg = *argv;
  532. while (*++arg) {
  533. switch (*arg) {
  534. case 'b': /* raw binary format */
  535. if (fmt++)
  536. goto sep_err;
  537. sep = '\0';
  538. break;
  539. case 'c': /* external checksum format */
  540. if (fmt++)
  541. goto sep_err;
  542. sep = '\0';
  543. chk = 1;
  544. break;
  545. case 't': /* text format */
  546. if (fmt++)
  547. goto sep_err;
  548. sep = '\n';
  549. break;
  550. default:
  551. return cmd_usage(cmdtp);
  552. }
  553. }
  554. }
  555. if (argc < 1) {
  556. return cmd_usage(cmdtp);
  557. }
  558. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  559. if (argc == 2) {
  560. size = simple_strtoul(argv[1], NULL, 16);
  561. memset(addr, '\0', size);
  562. } else {
  563. size = 0;
  564. }
  565. if (sep) { /* export as text file */
  566. len = hexport_r(&env_htab, sep, &addr, size);
  567. if (len < 0) {
  568. error("Cannot export environment: errno = %d\n",
  569. errno);
  570. return 1;
  571. }
  572. sprintf(buf, "%zX", (size_t)len);
  573. setenv("filesize", buf);
  574. return 0;
  575. }
  576. envp = (env_t *)addr;
  577. if (chk) /* export as checksum protected block */
  578. res = (char *)envp->data;
  579. else /* export as raw binary data */
  580. res = addr;
  581. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
  582. if (len < 0) {
  583. error("Cannot export environment: errno = %d\n",
  584. errno);
  585. return 1;
  586. }
  587. if (chk) {
  588. envp->crc = crc32(0, envp->data, ENV_SIZE);
  589. #ifdef CONFIG_ENV_ADDR_REDUND
  590. envp->flags = ACTIVE_FLAG;
  591. #endif
  592. }
  593. sprintf(buf, "%zX", (size_t)(len + offsetof(env_t,data)));
  594. setenv("filesize", buf);
  595. return 0;
  596. sep_err:
  597. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  598. cmd);
  599. return 1;
  600. }
  601. #endif
  602. #ifdef CONFIG_CMD_IMPORTENV
  603. /*
  604. * env import [-d] [-t | -b | -c] addr [size]
  605. * -d: delete existing environment before importing;
  606. * otherwise overwrite / append to existion definitions
  607. * -t: assume text format; either "size" must be given or the
  608. * text data must be '\0' terminated
  609. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  610. * -c: assume checksum protected environment format
  611. * addr: memory address to read from
  612. * size: length of input data; if missing, proper '\0'
  613. * termination is mandatory
  614. */
  615. static int do_env_import(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  616. {
  617. char *cmd, *addr;
  618. char sep = '\n';
  619. int chk = 0;
  620. int fmt = 0;
  621. int del = 0;
  622. size_t size;
  623. cmd = *argv;
  624. while (--argc > 0 && **++argv == '-') {
  625. char *arg = *argv;
  626. while (*++arg) {
  627. switch (*arg) {
  628. case 'b': /* raw binary format */
  629. if (fmt++)
  630. goto sep_err;
  631. sep = '\0';
  632. break;
  633. case 'c': /* external checksum format */
  634. if (fmt++)
  635. goto sep_err;
  636. sep = '\0';
  637. chk = 1;
  638. break;
  639. case 't': /* text format */
  640. if (fmt++)
  641. goto sep_err;
  642. sep = '\n';
  643. break;
  644. case 'd':
  645. del = 1;
  646. break;
  647. default:
  648. return cmd_usage(cmdtp);
  649. }
  650. }
  651. }
  652. if (argc < 1) {
  653. return cmd_usage(cmdtp);
  654. }
  655. if (!fmt)
  656. printf("## Warning: defaulting to text format\n");
  657. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  658. if (argc == 2) {
  659. size = simple_strtoul(argv[1], NULL, 16);
  660. } else {
  661. char *s = addr;
  662. size = 0;
  663. while (size < MAX_ENV_SIZE) {
  664. if ((*s == sep) && (*(s+1) == '\0'))
  665. break;
  666. ++s;
  667. ++size;
  668. }
  669. if (size == MAX_ENV_SIZE) {
  670. printf("## Warning: Input data exceeds %d bytes"
  671. " - truncated\n", MAX_ENV_SIZE);
  672. }
  673. ++size;
  674. printf("## Info: input data size = %zd = 0x%zX\n", size, size);
  675. }
  676. if (chk) {
  677. uint32_t crc;
  678. env_t *ep = (env_t *)addr;
  679. size -= offsetof(env_t, data);
  680. memcpy(&crc, &ep->crc, sizeof(crc));
  681. if (crc32(0, ep->data, size) != crc) {
  682. puts("## Error: bad CRC, import failed\n");
  683. return 1;
  684. }
  685. addr = (char *)ep->data;
  686. }
  687. if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR) == 0) {
  688. error("Environment import failed: errno = %d\n", errno);
  689. return 1;
  690. }
  691. gd->flags |= GD_FLG_ENV_READY;
  692. return 0;
  693. sep_err:
  694. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  695. cmd);
  696. return 1;
  697. }
  698. #endif
  699. #if defined(CONFIG_CMD_RUN)
  700. extern int do_run (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  701. #endif
  702. /*
  703. * New command line interface: "env" command with subcommands
  704. */
  705. static cmd_tbl_t cmd_env_sub[] = {
  706. #if defined(CONFIG_CMD_ASKENV)
  707. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  708. #endif
  709. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  710. U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
  711. #if defined(CONFIG_CMD_EDITENV)
  712. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  713. #endif
  714. #if defined(CONFIG_CMD_EXPORTENV)
  715. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  716. #endif
  717. #if defined(CONFIG_CMD_IMPORTENV)
  718. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  719. #endif
  720. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  721. #if defined(CONFIG_CMD_RUN)
  722. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  723. #endif
  724. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  725. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  726. #endif
  727. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  728. };
  729. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  730. void env_reloc(void)
  731. {
  732. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  733. }
  734. #endif
  735. static int do_env (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  736. {
  737. cmd_tbl_t *cp;
  738. if (argc < 2)
  739. return cmd_usage(cmdtp);
  740. /* drop initial "env" arg */
  741. argc--;
  742. argv++;
  743. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  744. if (cp)
  745. return cp->cmd(cmdtp, flag, argc, argv);
  746. return cmd_usage(cmdtp);
  747. }
  748. U_BOOT_CMD(
  749. env, CONFIG_SYS_MAXARGS, 1, do_env,
  750. "environment handling commands",
  751. #if defined(CONFIG_CMD_ASKENV)
  752. "ask name [message] [size] - ask for environment variable\nenv "
  753. #endif
  754. "default -f - reset default environment\n"
  755. #if defined(CONFIG_CMD_EDITENV)
  756. "env edit name - edit environment variable\n"
  757. #endif
  758. "env export [-t | -b | -c] addr [size] - export environmnt\n"
  759. "env import [-d] [-t | -b | -c] addr [size] - import environmnt\n"
  760. "env print [name ...] - print environment\n"
  761. #if defined(CONFIG_CMD_RUN)
  762. "env run var [...] - run commands in an environment variable\n"
  763. #endif
  764. "env save - save environment\n"
  765. "env set [-f] name [arg ...]\n"
  766. );
  767. /*
  768. * Old command line interface, kept for compatibility
  769. */
  770. #if defined(CONFIG_CMD_EDITENV)
  771. U_BOOT_CMD_COMPLETE(
  772. editenv, 2, 0, do_env_edit,
  773. "edit environment variable",
  774. "name\n"
  775. " - edit environment variable 'name'",
  776. var_complete
  777. );
  778. #endif
  779. U_BOOT_CMD_COMPLETE(
  780. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  781. "print environment variables",
  782. "\n - print values of all environment variables\n"
  783. "printenv name ...\n"
  784. " - print value of environment variable 'name'",
  785. var_complete
  786. );
  787. U_BOOT_CMD_COMPLETE(
  788. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  789. "set environment variables",
  790. "name value ...\n"
  791. " - set environment variable 'name' to 'value ...'\n"
  792. "setenv name\n"
  793. " - delete environment variable 'name'",
  794. var_complete
  795. );
  796. #if defined(CONFIG_CMD_ASKENV)
  797. U_BOOT_CMD(
  798. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  799. "get environment variables from stdin",
  800. "name [message] [size]\n"
  801. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  802. "askenv name\n"
  803. " - get environment variable 'name' from stdin\n"
  804. "askenv name size\n"
  805. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  806. "askenv name [message] size\n"
  807. " - display 'message' string and get environment variable 'name'"
  808. "from stdin (max 'size' chars)"
  809. );
  810. #endif
  811. #if defined(CONFIG_CMD_RUN)
  812. U_BOOT_CMD_COMPLETE(
  813. run, CONFIG_SYS_MAXARGS, 1, do_run,
  814. "run commands in an environment variable",
  815. "var [...]\n"
  816. " - run the commands in the environment variable(s) 'var'",
  817. var_complete
  818. );
  819. #endif