cmd_nvedit.c 25 KB

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