cmd_nvedit.c 28 KB

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