cmd_nvedit.c 24 KB

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