cmd_nvedit.c 26 KB

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