cmd_nvedit.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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);
  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) {
  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);
  311. /*
  312. * Perform requested checks. Notice how since we are overwriting
  313. * a single variable, we need to set H_NOCLEAR
  314. */
  315. if (env_check_apply(name, ep ? ep->data : NULL, value, H_NOCLEAR)) {
  316. debug("check function did not approve, refusing\n");
  317. return 1;
  318. }
  319. /* Delete only ? */
  320. if (argc < 3 || argv[2] == NULL) {
  321. int rc = hdelete_r(name, &env_htab, 0);
  322. return !rc;
  323. }
  324. /*
  325. * Insert / replace new value
  326. */
  327. for (i = 2, len = 0; i < argc; ++i)
  328. len += strlen(argv[i]) + 1;
  329. value = malloc(len);
  330. if (value == NULL) {
  331. printf("## Can't malloc %d bytes\n", len);
  332. return 1;
  333. }
  334. for (i = 2, s = value; i < argc; ++i) {
  335. char *v = argv[i];
  336. while ((*s++ = *v++) != '\0')
  337. ;
  338. *(s - 1) = ' ';
  339. }
  340. if (s != value)
  341. *--s = '\0';
  342. e.key = name;
  343. e.data = value;
  344. hsearch_r(e, ENTER, &ep, &env_htab);
  345. free(value);
  346. if (!ep) {
  347. printf("## Error inserting \"%s\" variable, errno=%d\n",
  348. name, errno);
  349. return 1;
  350. }
  351. return 0;
  352. }
  353. int setenv(const char *varname, const char *varvalue)
  354. {
  355. const char * const argv[4] = { "setenv", varname, varvalue, NULL };
  356. if (varvalue == NULL || varvalue[0] == '\0')
  357. return _do_env_set(0, 2, (char * const *)argv);
  358. else
  359. return _do_env_set(0, 3, (char * const *)argv);
  360. }
  361. /**
  362. * Set an environment variable to an integer value
  363. *
  364. * @param varname Environmet variable to set
  365. * @param value Value to set it to
  366. * @return 0 if ok, 1 on error
  367. */
  368. int setenv_ulong(const char *varname, ulong value)
  369. {
  370. /* TODO: this should be unsigned */
  371. char *str = simple_itoa(value);
  372. return setenv(varname, str);
  373. }
  374. /**
  375. * Set an environment variable to an address in hex
  376. *
  377. * @param varname Environmet variable to set
  378. * @param addr Value to set it to
  379. * @return 0 if ok, 1 on error
  380. */
  381. int setenv_addr(const char *varname, const void *addr)
  382. {
  383. char str[17];
  384. sprintf(str, "%lx", (uintptr_t)addr);
  385. return setenv(varname, str);
  386. }
  387. #ifndef CONFIG_SPL_BUILD
  388. static 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. static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
  452. char * const argv[])
  453. {
  454. char buffer[CONFIG_SYS_CBSIZE];
  455. char *init_val;
  456. if (argc < 2)
  457. return CMD_RET_USAGE;
  458. /* Set read buffer to initial value or empty sting */
  459. init_val = getenv(argv[1]);
  460. if (init_val)
  461. sprintf(buffer, "%s", init_val);
  462. else
  463. buffer[0] = '\0';
  464. readline_into_buffer("edit: ", buffer, 0);
  465. return setenv(argv[1], buffer);
  466. }
  467. #endif /* CONFIG_CMD_EDITENV */
  468. #endif /* CONFIG_SPL_BUILD */
  469. /*
  470. * Look up variable from environment,
  471. * return address of storage for that variable,
  472. * or NULL if not found
  473. */
  474. char *getenv(const char *name)
  475. {
  476. if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
  477. ENTRY e, *ep;
  478. WATCHDOG_RESET();
  479. e.key = name;
  480. e.data = NULL;
  481. hsearch_r(e, FIND, &ep, &env_htab);
  482. return ep ? ep->data : NULL;
  483. }
  484. /* restricted capabilities before import */
  485. if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
  486. return (char *)(gd->env_buf);
  487. return NULL;
  488. }
  489. /*
  490. * Look up variable from environment for restricted C runtime env.
  491. */
  492. int getenv_f(const char *name, char *buf, unsigned len)
  493. {
  494. int i, nxt;
  495. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  496. int val, n;
  497. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
  498. if (nxt >= CONFIG_ENV_SIZE)
  499. return -1;
  500. }
  501. val = envmatch((uchar *)name, i);
  502. if (val < 0)
  503. continue;
  504. /* found; copy out */
  505. for (n = 0; n < len; ++n, ++buf) {
  506. *buf = env_get_char(val++);
  507. if (*buf == '\0')
  508. return n;
  509. }
  510. if (n)
  511. *--buf = '\0';
  512. printf("env_buf [%d bytes] too small for value of \"%s\"\n",
  513. len, name);
  514. return n;
  515. }
  516. return -1;
  517. }
  518. /**
  519. * Decode the integer value of an environment variable and return it.
  520. *
  521. * @param name Name of environemnt variable
  522. * @param base Number base to use (normally 10, or 16 for hex)
  523. * @param default_val Default value to return if the variable is not
  524. * found
  525. * @return the decoded value, or default_val if not found
  526. */
  527. ulong getenv_ulong(const char *name, int base, ulong default_val)
  528. {
  529. /*
  530. * We can use getenv() here, even before relocation, since the
  531. * environment variable value is an integer and thus short.
  532. */
  533. const char *str = getenv(name);
  534. return str ? simple_strtoul(str, NULL, base) : default_val;
  535. }
  536. #ifndef CONFIG_SPL_BUILD
  537. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  538. static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
  539. char * const argv[])
  540. {
  541. printf("Saving Environment to %s...\n", env_name_spec);
  542. return saveenv() ? 1 : 0;
  543. }
  544. U_BOOT_CMD(
  545. saveenv, 1, 0, do_env_save,
  546. "save environment variables to persistent storage",
  547. ""
  548. );
  549. #endif
  550. #endif /* CONFIG_SPL_BUILD */
  551. /*
  552. * Match a name / name=value pair
  553. *
  554. * s1 is either a simple 'name', or a 'name=value' pair.
  555. * i2 is the environment index for a 'name2=value2' pair.
  556. * If the names match, return the index for the value2, else -1.
  557. */
  558. int envmatch(uchar *s1, int i2)
  559. {
  560. if (s1 == NULL)
  561. return -1;
  562. while (*s1 == env_get_char(i2++))
  563. if (*s1++ == '=')
  564. return i2;
  565. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  566. return i2;
  567. return -1;
  568. }
  569. #ifndef CONFIG_SPL_BUILD
  570. static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
  571. int argc, char * const argv[])
  572. {
  573. int all = 0, flag = 0;
  574. debug("Initial value for argc=%d\n", argc);
  575. while (--argc > 0 && **++argv == '-') {
  576. char *arg = *argv;
  577. while (*++arg) {
  578. switch (*arg) {
  579. case 'a': /* default all */
  580. all = 1;
  581. break;
  582. case 'f': /* force */
  583. flag |= H_FORCE;
  584. break;
  585. default:
  586. return cmd_usage(cmdtp);
  587. }
  588. }
  589. }
  590. debug("Final value for argc=%d\n", argc);
  591. if (all && (argc == 0)) {
  592. /* Reset the whole environment */
  593. set_default_env("## Resetting to default environment\n");
  594. return 0;
  595. }
  596. if (!all && (argc > 0)) {
  597. /* Reset individual variables */
  598. set_default_vars(argc, argv);
  599. return 0;
  600. }
  601. return cmd_usage(cmdtp);
  602. }
  603. static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
  604. int argc, char * const argv[])
  605. {
  606. printf("Not implemented yet\n");
  607. return 0;
  608. }
  609. #ifdef CONFIG_CMD_EXPORTENV
  610. /*
  611. * env export [-t | -b | -c] [-s size] addr [var ...]
  612. * -t: export as text format; if size is given, data will be
  613. * padded with '\0' bytes; if not, one terminating '\0'
  614. * will be added (which is included in the "filesize"
  615. * setting so you can for exmple copy this to flash and
  616. * keep the termination).
  617. * -b: export as binary format (name=value pairs separated by
  618. * '\0', list end marked by double "\0\0")
  619. * -c: export as checksum protected environment format as
  620. * used for example by "saveenv" command
  621. * -s size:
  622. * size of output buffer
  623. * addr: memory address where environment gets stored
  624. * var... List of variable names that get included into the
  625. * export. Without arguments, the whole environment gets
  626. * exported.
  627. *
  628. * With "-c" and size is NOT given, then the export command will
  629. * format the data as currently used for the persistent storage,
  630. * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
  631. * prepend a valid CRC32 checksum and, in case of resundant
  632. * environment, a "current" redundancy flag. If size is given, this
  633. * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
  634. * checksum and redundancy flag will be inserted.
  635. *
  636. * With "-b" and "-t", always only the real data (including a
  637. * terminating '\0' byte) will be written; here the optional size
  638. * argument will be used to make sure not to overflow the user
  639. * provided buffer; the command will abort if the size is not
  640. * sufficient. Any remainign space will be '\0' padded.
  641. *
  642. * On successful return, the variable "filesize" will be set.
  643. * Note that filesize includes the trailing/terminating '\0' byte(s).
  644. *
  645. * Usage szenario: create a text snapshot/backup of the current settings:
  646. *
  647. * => env export -t 100000
  648. * => era ${backup_addr} +${filesize}
  649. * => cp.b 100000 ${backup_addr} ${filesize}
  650. *
  651. * Re-import this snapshot, deleting all other settings:
  652. *
  653. * => env import -d -t ${backup_addr}
  654. */
  655. static int do_env_export(cmd_tbl_t *cmdtp, int flag,
  656. int argc, char * const argv[])
  657. {
  658. char buf[32];
  659. char *addr, *cmd, *res;
  660. size_t size = 0;
  661. ssize_t len;
  662. env_t *envp;
  663. char sep = '\n';
  664. int chk = 0;
  665. int fmt = 0;
  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 's': /* size given */
  683. if (--argc <= 0)
  684. return cmd_usage(cmdtp);
  685. size = simple_strtoul(*++argv, NULL, 16);
  686. goto NXTARG;
  687. case 't': /* text format */
  688. if (fmt++)
  689. goto sep_err;
  690. sep = '\n';
  691. break;
  692. default:
  693. return CMD_RET_USAGE;
  694. }
  695. }
  696. NXTARG: ;
  697. }
  698. if (argc < 1)
  699. return CMD_RET_USAGE;
  700. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  701. if (size)
  702. memset(addr, '\0', size);
  703. argc--;
  704. argv++;
  705. if (sep) { /* export as text file */
  706. len = hexport_r(&env_htab, sep, &addr, size, argc, argv);
  707. if (len < 0) {
  708. error("Cannot export environment: errno = %d\n", errno);
  709. return 1;
  710. }
  711. sprintf(buf, "%zX", (size_t)len);
  712. setenv("filesize", buf);
  713. return 0;
  714. }
  715. envp = (env_t *)addr;
  716. if (chk) /* export as checksum protected block */
  717. res = (char *)envp->data;
  718. else /* export as raw binary data */
  719. res = addr;
  720. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, argc, argv);
  721. if (len < 0) {
  722. error("Cannot export environment: errno = %d\n", errno);
  723. return 1;
  724. }
  725. if (chk) {
  726. envp->crc = crc32(0, envp->data, ENV_SIZE);
  727. #ifdef CONFIG_ENV_ADDR_REDUND
  728. envp->flags = ACTIVE_FLAG;
  729. #endif
  730. }
  731. sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
  732. setenv("filesize", buf);
  733. return 0;
  734. sep_err:
  735. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
  736. return 1;
  737. }
  738. #endif
  739. #ifdef CONFIG_CMD_IMPORTENV
  740. /*
  741. * env import [-d] [-t | -b | -c] addr [size]
  742. * -d: delete existing environment before importing;
  743. * otherwise overwrite / append to existion definitions
  744. * -t: assume text format; either "size" must be given or the
  745. * text data must be '\0' terminated
  746. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  747. * -c: assume checksum protected environment format
  748. * addr: memory address to read from
  749. * size: length of input data; if missing, proper '\0'
  750. * termination is mandatory
  751. */
  752. static int do_env_import(cmd_tbl_t *cmdtp, int flag,
  753. int argc, char * const argv[])
  754. {
  755. char *cmd, *addr;
  756. char sep = '\n';
  757. int chk = 0;
  758. int fmt = 0;
  759. int del = 0;
  760. size_t size;
  761. cmd = *argv;
  762. while (--argc > 0 && **++argv == '-') {
  763. char *arg = *argv;
  764. while (*++arg) {
  765. switch (*arg) {
  766. case 'b': /* raw binary format */
  767. if (fmt++)
  768. goto sep_err;
  769. sep = '\0';
  770. break;
  771. case 'c': /* external checksum format */
  772. if (fmt++)
  773. goto sep_err;
  774. sep = '\0';
  775. chk = 1;
  776. break;
  777. case 't': /* text format */
  778. if (fmt++)
  779. goto sep_err;
  780. sep = '\n';
  781. break;
  782. case 'd':
  783. del = 1;
  784. break;
  785. default:
  786. return CMD_RET_USAGE;
  787. }
  788. }
  789. }
  790. if (argc < 1)
  791. return CMD_RET_USAGE;
  792. if (!fmt)
  793. printf("## Warning: defaulting to text format\n");
  794. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  795. if (argc == 2) {
  796. size = simple_strtoul(argv[1], NULL, 16);
  797. } else {
  798. char *s = addr;
  799. size = 0;
  800. while (size < MAX_ENV_SIZE) {
  801. if ((*s == sep) && (*(s+1) == '\0'))
  802. break;
  803. ++s;
  804. ++size;
  805. }
  806. if (size == MAX_ENV_SIZE) {
  807. printf("## Warning: Input data exceeds %d bytes"
  808. " - truncated\n", MAX_ENV_SIZE);
  809. }
  810. size += 2;
  811. printf("## Info: input data size = %zu = 0x%zX\n", size, size);
  812. }
  813. if (chk) {
  814. uint32_t crc;
  815. env_t *ep = (env_t *)addr;
  816. size -= offsetof(env_t, data);
  817. memcpy(&crc, &ep->crc, sizeof(crc));
  818. if (crc32(0, ep->data, size) != crc) {
  819. puts("## Error: bad CRC, import failed\n");
  820. return 1;
  821. }
  822. addr = (char *)ep->data;
  823. }
  824. if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
  825. 0, NULL, 0 /* do_apply */) == 0) {
  826. error("Environment import failed: errno = %d\n", errno);
  827. return 1;
  828. }
  829. gd->flags |= GD_FLG_ENV_READY;
  830. return 0;
  831. sep_err:
  832. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  833. cmd);
  834. return 1;
  835. }
  836. #endif
  837. /*
  838. * New command line interface: "env" command with subcommands
  839. */
  840. static cmd_tbl_t cmd_env_sub[] = {
  841. #if defined(CONFIG_CMD_ASKENV)
  842. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  843. #endif
  844. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  845. U_BOOT_CMD_MKENT(delete, 2, 0, do_env_delete, "", ""),
  846. #if defined(CONFIG_CMD_EDITENV)
  847. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  848. #endif
  849. #if defined(CONFIG_CMD_EXPORTENV)
  850. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  851. #endif
  852. #if defined(CONFIG_CMD_GREPENV)
  853. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  854. #endif
  855. #if defined(CONFIG_CMD_IMPORTENV)
  856. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  857. #endif
  858. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  859. #if defined(CONFIG_CMD_RUN)
  860. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  861. #endif
  862. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  863. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  864. #endif
  865. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  866. };
  867. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  868. void env_reloc(void)
  869. {
  870. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  871. }
  872. #endif
  873. static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  874. {
  875. cmd_tbl_t *cp;
  876. if (argc < 2)
  877. return CMD_RET_USAGE;
  878. /* drop initial "env" arg */
  879. argc--;
  880. argv++;
  881. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  882. if (cp)
  883. return cp->cmd(cmdtp, flag, argc, argv);
  884. return CMD_RET_USAGE;
  885. }
  886. #ifdef CONFIG_SYS_LONGHELP
  887. static char env_help_text[] =
  888. #if defined(CONFIG_CMD_ASKENV)
  889. "ask name [message] [size] - ask for environment variable\nenv "
  890. #endif
  891. "default [-f] -a - [forcibly] reset default environment\n"
  892. "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
  893. #if defined(CONFIG_CMD_EDITENV)
  894. "env edit name - edit environment variable\n"
  895. #endif
  896. #if defined(CONFIG_CMD_EXPORTENV)
  897. "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  898. #endif
  899. #if defined(CONFIG_CMD_GREPENV)
  900. "env grep string [...] - search environment\n"
  901. #endif
  902. #if defined(CONFIG_CMD_IMPORTENV)
  903. "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
  904. #endif
  905. "env print [name ...] - print environment\n"
  906. #if defined(CONFIG_CMD_RUN)
  907. "env run var [...] - run commands in an environment variable\n"
  908. #endif
  909. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  910. "env save - save environment\n"
  911. #endif
  912. "env set [-f] name [arg ...]\n";
  913. #endif
  914. U_BOOT_CMD(
  915. env, CONFIG_SYS_MAXARGS, 1, do_env,
  916. "environment handling commands", env_help_text
  917. );
  918. /*
  919. * Old command line interface, kept for compatibility
  920. */
  921. #if defined(CONFIG_CMD_EDITENV)
  922. U_BOOT_CMD_COMPLETE(
  923. editenv, 2, 0, do_env_edit,
  924. "edit environment variable",
  925. "name\n"
  926. " - edit environment variable 'name'",
  927. var_complete
  928. );
  929. #endif
  930. U_BOOT_CMD_COMPLETE(
  931. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  932. "print environment variables",
  933. "\n - print values of all environment variables\n"
  934. "printenv name ...\n"
  935. " - print value of environment variable 'name'",
  936. var_complete
  937. );
  938. #ifdef CONFIG_CMD_GREPENV
  939. U_BOOT_CMD_COMPLETE(
  940. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  941. "search environment variables",
  942. "string ...\n"
  943. " - list environment name=value pairs matching 'string'",
  944. var_complete
  945. );
  946. #endif
  947. U_BOOT_CMD_COMPLETE(
  948. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  949. "set environment variables",
  950. "name value ...\n"
  951. " - set environment variable 'name' to 'value ...'\n"
  952. "setenv name\n"
  953. " - delete environment variable 'name'",
  954. var_complete
  955. );
  956. #if defined(CONFIG_CMD_ASKENV)
  957. U_BOOT_CMD(
  958. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  959. "get environment variables from stdin",
  960. "name [message] [size]\n"
  961. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  962. "askenv name\n"
  963. " - get environment variable 'name' from stdin\n"
  964. "askenv name size\n"
  965. " - get environment variable 'name' from stdin (max 'size' chars)\n"
  966. "askenv name [message] size\n"
  967. " - display 'message' string and get environment variable 'name'"
  968. "from stdin (max 'size' chars)"
  969. );
  970. #endif
  971. #if defined(CONFIG_CMD_RUN)
  972. U_BOOT_CMD_COMPLETE(
  973. run, CONFIG_SYS_MAXARGS, 1, do_run,
  974. "run commands in an environment variable",
  975. "var [...]\n"
  976. " - run the commands in the environment variable(s) 'var'",
  977. var_complete
  978. );
  979. #endif
  980. #endif /* CONFIG_SPL_BUILD */