cmd_nvedit.c 27 KB

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