nvedit.c 30 KB

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