cmd_nvedit.c 29 KB

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