cmd_nvedit.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  335. {
  336. if (argc < 2)
  337. return cmd_usage(cmdtp);
  338. return _do_env_set(flag, argc, argv);
  339. }
  340. /*
  341. * Prompt for environment variable
  342. */
  343. #if defined(CONFIG_CMD_ASKENV)
  344. int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  345. {
  346. extern char console_buffer[CONFIG_SYS_CBSIZE];
  347. char message[CONFIG_SYS_CBSIZE];
  348. int size = CONFIG_SYS_CBSIZE - 1;
  349. int i, len, pos;
  350. char *local_args[4];
  351. local_args[0] = argv[0];
  352. local_args[1] = argv[1];
  353. local_args[2] = NULL;
  354. local_args[3] = NULL;
  355. /* Check the syntax */
  356. switch (argc) {
  357. case 1:
  358. return cmd_usage(cmdtp);
  359. case 2: /* env_ask envname */
  360. sprintf(message, "Please enter '%s':", argv[1]);
  361. break;
  362. case 3: /* env_ask envname size */
  363. sprintf(message, "Please enter '%s':", argv[1]);
  364. size = simple_strtoul(argv[2], NULL, 10);
  365. break;
  366. default: /* env_ask envname message1 ... messagen size */
  367. for (i = 2, pos = 0; i < argc - 1; i++) {
  368. if (pos)
  369. message[pos++] = ' ';
  370. strcpy(message+pos, argv[i]);
  371. pos += strlen(argv[i]);
  372. }
  373. message[pos] = '\0';
  374. size = simple_strtoul(argv[argc - 1], NULL, 10);
  375. break;
  376. }
  377. if (size >= CONFIG_SYS_CBSIZE)
  378. size = CONFIG_SYS_CBSIZE - 1;
  379. if (size <= 0)
  380. return 1;
  381. /* prompt for input */
  382. len = readline(message);
  383. if (size < len)
  384. console_buffer[size] = '\0';
  385. len = 2;
  386. if (console_buffer[0] != '\0') {
  387. local_args[2] = console_buffer;
  388. len = 3;
  389. }
  390. /* Continue calling setenv code */
  391. return _do_env_set(flag, len, local_args);
  392. }
  393. #endif
  394. /*
  395. * Interactively edit an environment variable
  396. */
  397. #if defined(CONFIG_CMD_EDITENV)
  398. int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  399. {
  400. char buffer[CONFIG_SYS_CBSIZE];
  401. char *init_val;
  402. if (argc < 2)
  403. return cmd_usage(cmdtp);
  404. /* Set read buffer to initial value or empty sting */
  405. init_val = getenv(argv[1]);
  406. if (init_val)
  407. sprintf(buffer, "%s", init_val);
  408. else
  409. buffer[0] = '\0';
  410. readline_into_buffer("edit: ", buffer);
  411. return setenv(argv[1], buffer);
  412. }
  413. #endif /* CONFIG_CMD_EDITENV */
  414. /*
  415. * Look up variable from environment,
  416. * return address of storage for that variable,
  417. * or NULL if not found
  418. */
  419. char *getenv(const char *name)
  420. {
  421. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  422. ENTRY e, *ep;
  423. WATCHDOG_RESET();
  424. e.key = name;
  425. e.data = NULL;
  426. hsearch_r(e, FIND, &ep, &env_htab);
  427. return ep ? ep->data : NULL;
  428. }
  429. /* restricted capabilities before import */
  430. if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
  431. return (char *)(gd->env_buf);
  432. return NULL;
  433. }
  434. /*
  435. * Look up variable from environment for restricted C runtime env.
  436. */
  437. int getenv_f(const char *name, char *buf, unsigned len)
  438. {
  439. int i, nxt;
  440. for (i = 0; env_get_char(i) != '\0'; i = nxt+1) {
  441. int val, n;
  442. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
  443. if (nxt >= CONFIG_ENV_SIZE)
  444. return -1;
  445. }
  446. val = envmatch((uchar *)name, i);
  447. if (val < 0)
  448. continue;
  449. /* found; copy out */
  450. for (n = 0; n < len; ++n, ++buf) {
  451. if ((*buf = env_get_char(val++)) == '\0')
  452. return n;
  453. }
  454. if (n)
  455. *--buf = '\0';
  456. printf("env_buf [%d bytes] too small for value of \"%s\"\n",
  457. len, name);
  458. return n;
  459. }
  460. return -1;
  461. }
  462. /**
  463. * Decode the integer value of an environment variable and return it.
  464. *
  465. * @param name Name of environemnt variable
  466. * @param base Number base to use (normally 10, or 16 for hex)
  467. * @param default_val Default value to return if the variable is not
  468. * found
  469. * @return the decoded value, or default_val if not found
  470. */
  471. ulong getenv_ulong(const char *name, int base, ulong default_val)
  472. {
  473. /*
  474. * We can use getenv() here, even before relocation, since the
  475. * environment variable value is an integer and thus short.
  476. */
  477. const char *str = getenv(name);
  478. return str ? simple_strtoul(str, NULL, base) : default_val;
  479. }
  480. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  481. int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  482. {
  483. extern char *env_name_spec;
  484. printf("Saving Environment to %s...\n", env_name_spec);
  485. return saveenv() ? 1 : 0;
  486. }
  487. U_BOOT_CMD(
  488. saveenv, 1, 0, do_env_save,
  489. "save environment variables to persistent storage",
  490. ""
  491. );
  492. #endif
  493. /*
  494. * Match a name / name=value pair
  495. *
  496. * s1 is either a simple 'name', or a 'name=value' pair.
  497. * i2 is the environment index for a 'name2=value2' pair.
  498. * If the names match, return the index for the value2, else NULL.
  499. */
  500. int envmatch(uchar *s1, int i2)
  501. {
  502. while (*s1 == env_get_char(i2++))
  503. if (*s1++ == '=')
  504. return i2;
  505. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  506. return i2;
  507. return -1;
  508. }
  509. static int do_env_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  510. {
  511. if ((argc != 2) || (strcmp(argv[1], "-f") != 0))
  512. return cmd_usage(cmdtp);
  513. set_default_env("## Resetting to default environment\n");
  514. return 0;
  515. }
  516. static int do_env_delete(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  517. {
  518. printf("Not implemented yet\n");
  519. return 0;
  520. }
  521. #ifdef CONFIG_CMD_EXPORTENV
  522. /*
  523. * env export [-t | -b | -c] addr [size]
  524. * -t: export as text format; if size is given, data will be
  525. * padded with '\0' bytes; if not, one terminating '\0'
  526. * will be added (which is included in the "filesize"
  527. * setting so you can for exmple copy this to flash and
  528. * keep the termination).
  529. * -b: export as binary format (name=value pairs separated by
  530. * '\0', list end marked by double "\0\0")
  531. * -c: export as checksum protected environment format as
  532. * used for example by "saveenv" command
  533. * addr: memory address where environment gets stored
  534. * size: size of output buffer
  535. *
  536. * With "-c" and size is NOT given, then the export command will
  537. * format the data as currently used for the persistent storage,
  538. * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
  539. * prepend a valid CRC32 checksum and, in case of resundant
  540. * environment, a "current" redundancy flag. If size is given, this
  541. * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
  542. * checksum and redundancy flag will be inserted.
  543. *
  544. * With "-b" and "-t", always only the real data (including a
  545. * terminating '\0' byte) will be written; here the optional size
  546. * argument will be used to make sure not to overflow the user
  547. * provided buffer; the command will abort if the size is not
  548. * sufficient. Any remainign space will be '\0' padded.
  549. *
  550. * On successful return, the variable "filesize" will be set.
  551. * Note that filesize includes the trailing/terminating '\0' byte(s).
  552. *
  553. * Usage szenario: create a text snapshot/backup of the current settings:
  554. *
  555. * => env export -t 100000
  556. * => era ${backup_addr} +${filesize}
  557. * => cp.b 100000 ${backup_addr} ${filesize}
  558. *
  559. * Re-import this snapshot, deleting all other settings:
  560. *
  561. * => env import -d -t ${backup_addr}
  562. */
  563. static int do_env_export(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  564. {
  565. char buf[32];
  566. char *addr, *cmd, *res;
  567. size_t size;
  568. ssize_t len;
  569. env_t *envp;
  570. char sep = '\n';
  571. int chk = 0;
  572. int fmt = 0;
  573. cmd = *argv;
  574. while (--argc > 0 && **++argv == '-') {
  575. char *arg = *argv;
  576. while (*++arg) {
  577. switch (*arg) {
  578. case 'b': /* raw binary format */
  579. if (fmt++)
  580. goto sep_err;
  581. sep = '\0';
  582. break;
  583. case 'c': /* external checksum format */
  584. if (fmt++)
  585. goto sep_err;
  586. sep = '\0';
  587. chk = 1;
  588. break;
  589. case 't': /* text format */
  590. if (fmt++)
  591. goto sep_err;
  592. sep = '\n';
  593. break;
  594. default:
  595. return cmd_usage(cmdtp);
  596. }
  597. }
  598. }
  599. if (argc < 1)
  600. return cmd_usage(cmdtp);
  601. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  602. if (argc == 2) {
  603. size = simple_strtoul(argv[1], NULL, 16);
  604. memset(addr, '\0', size);
  605. } else {
  606. size = 0;
  607. }
  608. if (sep) { /* export as text file */
  609. len = hexport_r(&env_htab, sep, &addr, size);
  610. if (len < 0) {
  611. error("Cannot export environment: errno = %d\n",
  612. errno);
  613. return 1;
  614. }
  615. sprintf(buf, "%zX", (size_t)len);
  616. setenv("filesize", buf);
  617. return 0;
  618. }
  619. envp = (env_t *)addr;
  620. if (chk) /* export as checksum protected block */
  621. res = (char *)envp->data;
  622. else /* export as raw binary data */
  623. res = addr;
  624. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
  625. if (len < 0) {
  626. error("Cannot export environment: errno = %d\n",
  627. errno);
  628. return 1;
  629. }
  630. if (chk) {
  631. envp->crc = crc32(0, envp->data, ENV_SIZE);
  632. #ifdef CONFIG_ENV_ADDR_REDUND
  633. envp->flags = ACTIVE_FLAG;
  634. #endif
  635. }
  636. sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
  637. setenv("filesize", buf);
  638. return 0;
  639. sep_err:
  640. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  641. cmd);
  642. return 1;
  643. }
  644. #endif
  645. #ifdef CONFIG_CMD_IMPORTENV
  646. /*
  647. * env import [-d] [-t | -b | -c] addr [size]
  648. * -d: delete existing environment before importing;
  649. * otherwise overwrite / append to existion definitions
  650. * -t: assume text format; either "size" must be given or the
  651. * text data must be '\0' terminated
  652. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  653. * -c: assume checksum protected environment format
  654. * addr: memory address to read from
  655. * size: length of input data; if missing, proper '\0'
  656. * termination is mandatory
  657. */
  658. static int do_env_import(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  659. {
  660. char *cmd, *addr;
  661. char sep = '\n';
  662. int chk = 0;
  663. int fmt = 0;
  664. int del = 0;
  665. size_t size;
  666. cmd = *argv;
  667. while (--argc > 0 && **++argv == '-') {
  668. char *arg = *argv;
  669. while (*++arg) {
  670. switch (*arg) {
  671. case 'b': /* raw binary format */
  672. if (fmt++)
  673. goto sep_err;
  674. sep = '\0';
  675. break;
  676. case 'c': /* external checksum format */
  677. if (fmt++)
  678. goto sep_err;
  679. sep = '\0';
  680. chk = 1;
  681. break;
  682. case 't': /* text format */
  683. if (fmt++)
  684. goto sep_err;
  685. sep = '\n';
  686. break;
  687. case 'd':
  688. del = 1;
  689. break;
  690. default:
  691. return cmd_usage(cmdtp);
  692. }
  693. }
  694. }
  695. if (argc < 1)
  696. return cmd_usage(cmdtp);
  697. if (!fmt)
  698. printf("## Warning: defaulting to text format\n");
  699. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  700. if (argc == 2) {
  701. size = simple_strtoul(argv[1], NULL, 16);
  702. } else {
  703. char *s = addr;
  704. size = 0;
  705. while (size < MAX_ENV_SIZE) {
  706. if ((*s == sep) && (*(s+1) == '\0'))
  707. break;
  708. ++s;
  709. ++size;
  710. }
  711. if (size == MAX_ENV_SIZE) {
  712. printf("## Warning: Input data exceeds %d bytes"
  713. " - truncated\n", MAX_ENV_SIZE);
  714. }
  715. ++size;
  716. printf("## Info: input data size = %zd = 0x%zX\n", size, size);
  717. }
  718. if (chk) {
  719. uint32_t crc;
  720. env_t *ep = (env_t *)addr;
  721. size -= offsetof(env_t, data);
  722. memcpy(&crc, &ep->crc, sizeof(crc));
  723. if (crc32(0, ep->data, size) != crc) {
  724. puts("## Error: bad CRC, import failed\n");
  725. return 1;
  726. }
  727. addr = (char *)ep->data;
  728. }
  729. if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR) == 0) {
  730. error("Environment import failed: errno = %d\n", errno);
  731. return 1;
  732. }
  733. gd->flags |= GD_FLG_ENV_READY;
  734. return 0;
  735. sep_err:
  736. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  737. cmd);
  738. return 1;
  739. }
  740. #endif
  741. #if defined(CONFIG_CMD_RUN)
  742. extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  743. #endif
  744. /*
  745. * New command line interface: "env" command with subcommands
  746. */
  747. static cmd_tbl_t cmd_env_sub[] = {
  748. #if defined(CONFIG_CMD_ASKENV)
  749. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  750. #endif
  751. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  752. U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
  753. #if defined(CONFIG_CMD_EDITENV)
  754. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  755. #endif
  756. #if defined(CONFIG_CMD_EXPORTENV)
  757. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  758. #endif
  759. #if defined(CONFIG_CMD_GREPENV)
  760. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  761. #endif
  762. #if defined(CONFIG_CMD_IMPORTENV)
  763. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  764. #endif
  765. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  766. #if defined(CONFIG_CMD_RUN)
  767. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  768. #endif
  769. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  770. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  771. #endif
  772. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  773. };
  774. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  775. void env_reloc(void)
  776. {
  777. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  778. }
  779. #endif
  780. static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  781. {
  782. cmd_tbl_t *cp;
  783. if (argc < 2)
  784. return cmd_usage(cmdtp);
  785. /* drop initial "env" arg */
  786. argc--;
  787. argv++;
  788. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  789. if (cp)
  790. return cp->cmd(cmdtp, flag, argc, argv);
  791. return cmd_usage(cmdtp);
  792. }
  793. U_BOOT_CMD(
  794. env, CONFIG_SYS_MAXARGS, 1, do_env,
  795. "environment handling commands",
  796. #if defined(CONFIG_CMD_ASKENV)
  797. "ask name [message] [size] - ask for environment variable\nenv "
  798. #endif
  799. "default -f - reset default environment\n"
  800. #if defined(CONFIG_CMD_EDITENV)
  801. "env edit name - edit environment variable\n"
  802. #endif
  803. "env export [-t | -b | -c] addr [size] - export environment\n"
  804. #if defined(CONFIG_CMD_GREPENV)
  805. "env grep string [...] - search environment\n"
  806. #endif
  807. "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
  808. "env print [name ...] - print environment\n"
  809. #if defined(CONFIG_CMD_RUN)
  810. "env run var [...] - run commands in an environment variable\n"
  811. #endif
  812. "env save - save environment\n"
  813. "env set [-f] name [arg ...]\n"
  814. );
  815. /*
  816. * Old command line interface, kept for compatibility
  817. */
  818. #if defined(CONFIG_CMD_EDITENV)
  819. U_BOOT_CMD_COMPLETE(
  820. editenv, 2, 0, do_env_edit,
  821. "edit environment variable",
  822. "name\n"
  823. " - edit environment variable 'name'",
  824. var_complete
  825. );
  826. #endif
  827. U_BOOT_CMD_COMPLETE(
  828. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  829. "print environment variables",
  830. "\n - print values of all environment variables\n"
  831. "printenv name ...\n"
  832. " - print value of environment variable 'name'",
  833. var_complete
  834. );
  835. #ifdef CONFIG_CMD_GREPENV
  836. U_BOOT_CMD_COMPLETE(
  837. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  838. "search environment variables",
  839. "string ...\n"
  840. " - list environment name=value pairs matching 'string'",
  841. var_complete
  842. );
  843. #endif
  844. U_BOOT_CMD_COMPLETE(
  845. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  846. "set environment variables",
  847. "name value ...\n"
  848. " - set environment variable 'name' to 'value ...'\n"
  849. "setenv name\n"
  850. " - delete environment variable 'name'",
  851. var_complete
  852. );
  853. #if defined(CONFIG_CMD_ASKENV)
  854. U_BOOT_CMD(
  855. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  856. "get environment variables from stdin",
  857. "name [message] [size]\n"
  858. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  859. "askenv name\n"
  860. " - get environment variable 'name' from stdin\n"
  861. "askenv name size\n"
  862. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  863. "askenv name [message] size\n"
  864. " - display 'message' string and get environment variable 'name'"
  865. "from stdin (max 'size' chars)"
  866. );
  867. #endif
  868. #if defined(CONFIG_CMD_RUN)
  869. U_BOOT_CMD_COMPLETE(
  870. run, CONFIG_SYS_MAXARGS, 1, do_run,
  871. "run commands in an environment variable",
  872. "var [...]\n"
  873. " - run the commands in the environment variable(s) 'var'",
  874. var_complete
  875. );
  876. #endif