cmd_nvedit.c 24 KB

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