cmd_nvedit.c 27 KB

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