cmd_nvedit.c 24 KB

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