nvedit.c 30 KB

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