cmd_nvedit.c 28 KB

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