cmd_nvedit.c 25 KB

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