cmd_nvedit.c 30 KB

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