cmd_nvedit.c 30 KB

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