cmd_nvedit.c 25 KB

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