cmd_nvedit.c 26 KB

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