cmd_nvedit.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. /*
  29. * Support for persistent environment data
  30. *
  31. * The "environment" is stored on external storage as a list of '\0'
  32. * terminated "name=value" strings. The end of the list is marked by
  33. * a double '\0'. The environment is preceeded by a 32 bit CRC over
  34. * the data part and, in case of redundant environment, a byte of
  35. * flags.
  36. *
  37. * This linearized representation will also be used before
  38. * relocation, i. e. as long as we don't have a full C runtime
  39. * environment. After that, we use a hash table.
  40. */
  41. #include <common.h>
  42. #include <command.h>
  43. #include <environment.h>
  44. #include <search.h>
  45. #include <errno.h>
  46. #include <malloc.h>
  47. #include <watchdog.h>
  48. #include <linux/stddef.h>
  49. #include <asm/byteorder.h>
  50. DECLARE_GLOBAL_DATA_PTR;
  51. #if !defined(CONFIG_ENV_IS_IN_EEPROM) && \
  52. !defined(CONFIG_ENV_IS_IN_FLASH) && \
  53. !defined(CONFIG_ENV_IS_IN_DATAFLASH) && \
  54. !defined(CONFIG_ENV_IS_IN_MMC) && \
  55. !defined(CONFIG_ENV_IS_IN_FAT) && \
  56. !defined(CONFIG_ENV_IS_IN_NAND) && \
  57. !defined(CONFIG_ENV_IS_IN_NVRAM) && \
  58. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  59. !defined(CONFIG_ENV_IS_IN_SPI_FLASH) && \
  60. !defined(CONFIG_ENV_IS_IN_REMOTE) && \
  61. !defined(CONFIG_ENV_IS_IN_UBI) && \
  62. !defined(CONFIG_ENV_IS_NOWHERE)
  63. # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
  64. SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
  65. #endif
  66. /*
  67. * Maximum expected input data size for import command
  68. */
  69. #define MAX_ENV_SIZE (1 << 20) /* 1 MiB */
  70. /*
  71. * This variable is incremented on each do_env_set(), so it can
  72. * be used via get_env_id() as an indication, if the environment
  73. * has changed or not. So it is possible to reread an environment
  74. * variable only if the environment was changed ... done so for
  75. * example in NetInitLoop()
  76. */
  77. static int env_id = 1;
  78. int get_env_id(void)
  79. {
  80. return env_id;
  81. }
  82. #ifndef CONFIG_SPL_BUILD
  83. /*
  84. * Command interface: print one or all environment variables
  85. *
  86. * Returns 0 in case of error, or length of printed string
  87. */
  88. static int env_print(char *name, int flag)
  89. {
  90. char *res = NULL;
  91. ssize_t len;
  92. if (name) { /* print a single name */
  93. ENTRY e, *ep;
  94. e.key = name;
  95. e.data = NULL;
  96. hsearch_r(e, FIND, &ep, &env_htab, flag);
  97. if (ep == NULL)
  98. return 0;
  99. len = printf("%s=%s\n", ep->key, ep->data);
  100. return len;
  101. }
  102. /* print whole list */
  103. len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
  104. if (len > 0) {
  105. puts(res);
  106. free(res);
  107. return len;
  108. }
  109. /* should never happen */
  110. printf("## Error: cannot export environment\n");
  111. return 0;
  112. }
  113. static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
  114. char * const argv[])
  115. {
  116. int i;
  117. int rcode = 0;
  118. int env_flag = H_HIDE_DOT;
  119. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
  120. argc--;
  121. argv++;
  122. env_flag &= ~H_HIDE_DOT;
  123. }
  124. if (argc == 1) {
  125. /* print all env vars */
  126. rcode = env_print(NULL, env_flag);
  127. if (!rcode)
  128. return 1;
  129. printf("\nEnvironment size: %d/%ld bytes\n",
  130. rcode, (ulong)ENV_SIZE);
  131. return 0;
  132. }
  133. /* print selected env vars */
  134. env_flag &= ~H_HIDE_DOT;
  135. for (i = 1; i < argc; ++i) {
  136. int rc = env_print(argv[i], env_flag);
  137. if (!rc) {
  138. printf("## Error: \"%s\" not defined\n", argv[i]);
  139. ++rcode;
  140. }
  141. }
  142. return rcode;
  143. }
  144. #ifdef CONFIG_CMD_GREPENV
  145. static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
  146. int argc, char * const argv[])
  147. {
  148. char *res = NULL;
  149. int len, grep_how, grep_what;
  150. if (argc < 2)
  151. return CMD_RET_USAGE;
  152. grep_how = H_MATCH_SUBSTR; /* default: substring search */
  153. grep_what = H_MATCH_BOTH; /* default: grep names and values */
  154. while (argc > 1 && **(argv + 1) == '-') {
  155. char *arg = *++argv;
  156. --argc;
  157. while (*++arg) {
  158. switch (*arg) {
  159. #ifdef CONFIG_REGEX
  160. case 'e': /* use regex matching */
  161. grep_how = H_MATCH_REGEX;
  162. break;
  163. #endif
  164. case 'n': /* grep for name */
  165. grep_what = H_MATCH_KEY;
  166. break;
  167. case 'v': /* grep for value */
  168. grep_what = H_MATCH_DATA;
  169. break;
  170. case 'b': /* grep for both */
  171. grep_what = H_MATCH_BOTH;
  172. break;
  173. case '-':
  174. goto DONE;
  175. default:
  176. return CMD_RET_USAGE;
  177. }
  178. }
  179. }
  180. DONE:
  181. len = hexport_r(&env_htab, '\n',
  182. flag | grep_what | grep_how,
  183. &res, 0, argc, argv);
  184. if (len > 0) {
  185. puts(res);
  186. free(res);
  187. }
  188. if (len < 2)
  189. return 1;
  190. return 0;
  191. }
  192. #endif
  193. #endif /* CONFIG_SPL_BUILD */
  194. /*
  195. * Set a new environment variable,
  196. * or replace or delete an existing one.
  197. */
  198. static int _do_env_set(int flag, int argc, char * const argv[])
  199. {
  200. int i, len;
  201. char *name, *value, *s;
  202. ENTRY e, *ep;
  203. int env_flag = H_INTERACTIVE;
  204. debug("Initial value for argc=%d\n", argc);
  205. while (argc > 1 && **(argv + 1) == '-') {
  206. char *arg = *++argv;
  207. --argc;
  208. while (*++arg) {
  209. switch (*arg) {
  210. case 'f': /* force */
  211. env_flag |= H_FORCE;
  212. break;
  213. default:
  214. return CMD_RET_USAGE;
  215. }
  216. }
  217. }
  218. debug("Final value for argc=%d\n", argc);
  219. name = argv[1];
  220. value = argv[2];
  221. if (strchr(name, '=')) {
  222. printf("## Error: illegal character '='"
  223. "in variable name \"%s\"\n", name);
  224. return 1;
  225. }
  226. env_id++;
  227. /* Delete only ? */
  228. if (argc < 3 || argv[2] == NULL) {
  229. int rc = hdelete_r(name, &env_htab, env_flag);
  230. return !rc;
  231. }
  232. /*
  233. * Insert / replace new value
  234. */
  235. for (i = 2, len = 0; i < argc; ++i)
  236. len += strlen(argv[i]) + 1;
  237. value = malloc(len);
  238. if (value == NULL) {
  239. printf("## Can't malloc %d bytes\n", len);
  240. return 1;
  241. }
  242. for (i = 2, s = value; i < argc; ++i) {
  243. char *v = argv[i];
  244. while ((*s++ = *v++) != '\0')
  245. ;
  246. *(s - 1) = ' ';
  247. }
  248. if (s != value)
  249. *--s = '\0';
  250. e.key = name;
  251. e.data = value;
  252. hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
  253. free(value);
  254. if (!ep) {
  255. printf("## Error inserting \"%s\" variable, errno=%d\n",
  256. name, errno);
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. int setenv(const char *varname, const char *varvalue)
  262. {
  263. const char * const argv[4] = { "setenv", varname, varvalue, NULL };
  264. /* before import into hashtable */
  265. if (!(gd->flags & GD_FLG_ENV_READY))
  266. return 1;
  267. if (varvalue == NULL || varvalue[0] == '\0')
  268. return _do_env_set(0, 2, (char * const *)argv);
  269. else
  270. return _do_env_set(0, 3, (char * const *)argv);
  271. }
  272. /**
  273. * Set an environment variable to an integer value
  274. *
  275. * @param varname Environment variable to set
  276. * @param value Value to set it to
  277. * @return 0 if ok, 1 on error
  278. */
  279. int setenv_ulong(const char *varname, ulong value)
  280. {
  281. /* TODO: this should be unsigned */
  282. char *str = simple_itoa(value);
  283. return setenv(varname, str);
  284. }
  285. /**
  286. * Set an environment variable to an value in hex
  287. *
  288. * @param varname Environment variable to set
  289. * @param value Value to set it to
  290. * @return 0 if ok, 1 on error
  291. */
  292. int setenv_hex(const char *varname, ulong value)
  293. {
  294. char str[17];
  295. sprintf(str, "%lx", value);
  296. return setenv(varname, str);
  297. }
  298. ulong getenv_hex(const char *varname, ulong default_val)
  299. {
  300. const char *s;
  301. ulong value;
  302. char *endp;
  303. s = getenv(varname);
  304. if (s)
  305. value = simple_strtoul(s, &endp, 16);
  306. if (!s || endp == s)
  307. return default_val;
  308. return value;
  309. }
  310. #ifndef CONFIG_SPL_BUILD
  311. static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  312. {
  313. if (argc < 2)
  314. return CMD_RET_USAGE;
  315. return _do_env_set(flag, argc, argv);
  316. }
  317. /*
  318. * Prompt for environment variable
  319. */
  320. #if defined(CONFIG_CMD_ASKENV)
  321. int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  322. {
  323. char message[CONFIG_SYS_CBSIZE];
  324. int i, len, pos, size;
  325. char *local_args[4];
  326. char *endptr;
  327. local_args[0] = argv[0];
  328. local_args[1] = argv[1];
  329. local_args[2] = NULL;
  330. local_args[3] = NULL;
  331. /*
  332. * Check the syntax:
  333. *
  334. * env_ask envname [message1 ...] [size]
  335. */
  336. if (argc == 1)
  337. return CMD_RET_USAGE;
  338. /*
  339. * We test the last argument if it can be converted
  340. * into a decimal number. If yes, we assume it's
  341. * the size. Otherwise we echo it as part of the
  342. * message.
  343. */
  344. i = simple_strtoul(argv[argc - 1], &endptr, 10);
  345. if (*endptr != '\0') { /* no size */
  346. size = CONFIG_SYS_CBSIZE - 1;
  347. } else { /* size given */
  348. size = i;
  349. --argc;
  350. }
  351. if (argc <= 2) {
  352. sprintf(message, "Please enter '%s': ", argv[1]);
  353. } else {
  354. /* env_ask envname message1 ... messagen [size] */
  355. for (i = 2, pos = 0; i < argc; i++) {
  356. if (pos)
  357. message[pos++] = ' ';
  358. strcpy(message + pos, argv[i]);
  359. pos += strlen(argv[i]);
  360. }
  361. message[pos++] = ' ';
  362. message[pos] = '\0';
  363. }
  364. if (size >= CONFIG_SYS_CBSIZE)
  365. size = CONFIG_SYS_CBSIZE - 1;
  366. if (size <= 0)
  367. return 1;
  368. /* prompt for input */
  369. len = readline(message);
  370. if (size < len)
  371. console_buffer[size] = '\0';
  372. len = 2;
  373. if (console_buffer[0] != '\0') {
  374. local_args[2] = console_buffer;
  375. len = 3;
  376. }
  377. /* Continue calling setenv code */
  378. return _do_env_set(flag, len, local_args);
  379. }
  380. #endif
  381. #if defined(CONFIG_CMD_ENV_CALLBACK)
  382. static int print_static_binding(const char *var_name, const char *callback_name)
  383. {
  384. printf("\t%-20s %-20s\n", var_name, callback_name);
  385. return 0;
  386. }
  387. static int print_active_callback(ENTRY *entry)
  388. {
  389. struct env_clbk_tbl *clbkp;
  390. int i;
  391. int num_callbacks;
  392. if (entry->callback == NULL)
  393. return 0;
  394. /* look up the callback in the linker-list */
  395. num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
  396. for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
  397. i < num_callbacks;
  398. i++, clbkp++) {
  399. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  400. if (entry->callback == clbkp->callback + gd->reloc_off)
  401. #else
  402. if (entry->callback == clbkp->callback)
  403. #endif
  404. break;
  405. }
  406. if (i == num_callbacks)
  407. /* this should probably never happen, but just in case... */
  408. printf("\t%-20s %p\n", entry->key, entry->callback);
  409. else
  410. printf("\t%-20s %-20s\n", entry->key, clbkp->name);
  411. return 0;
  412. }
  413. /*
  414. * Print the callbacks available and what they are bound to
  415. */
  416. int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  417. {
  418. struct env_clbk_tbl *clbkp;
  419. int i;
  420. int num_callbacks;
  421. /* Print the available callbacks */
  422. puts("Available callbacks:\n");
  423. puts("\tCallback Name\n");
  424. puts("\t-------------\n");
  425. num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
  426. for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
  427. i < num_callbacks;
  428. i++, clbkp++)
  429. printf("\t%s\n", clbkp->name);
  430. puts("\n");
  431. /* Print the static bindings that may exist */
  432. puts("Static callback bindings:\n");
  433. printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
  434. printf("\t%-20s %-20s\n", "-------------", "-------------");
  435. env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding);
  436. puts("\n");
  437. /* walk through each variable and print the callback if it has one */
  438. puts("Active callback bindings:\n");
  439. printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
  440. printf("\t%-20s %-20s\n", "-------------", "-------------");
  441. hwalk_r(&env_htab, print_active_callback);
  442. return 0;
  443. }
  444. #endif
  445. #if defined(CONFIG_CMD_ENV_FLAGS)
  446. static int print_static_flags(const char *var_name, const char *flags)
  447. {
  448. enum env_flags_vartype type = env_flags_parse_vartype(flags);
  449. enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
  450. printf("\t%-20s %-20s %-20s\n", var_name,
  451. env_flags_get_vartype_name(type),
  452. env_flags_get_varaccess_name(access));
  453. return 0;
  454. }
  455. static int print_active_flags(ENTRY *entry)
  456. {
  457. enum env_flags_vartype type;
  458. enum env_flags_varaccess access;
  459. if (entry->flags == 0)
  460. return 0;
  461. type = (enum env_flags_vartype)
  462. (entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
  463. access = env_flags_parse_varaccess_from_binflags(entry->flags);
  464. printf("\t%-20s %-20s %-20s\n", entry->key,
  465. env_flags_get_vartype_name(type),
  466. env_flags_get_varaccess_name(access));
  467. return 0;
  468. }
  469. /*
  470. * Print the flags available and what variables have flags
  471. */
  472. int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  473. {
  474. /* Print the available variable types */
  475. printf("Available variable type flags (position %d):\n",
  476. ENV_FLAGS_VARTYPE_LOC);
  477. puts("\tFlag\tVariable Type Name\n");
  478. puts("\t----\t------------------\n");
  479. env_flags_print_vartypes();
  480. puts("\n");
  481. /* Print the available variable access types */
  482. printf("Available variable access flags (position %d):\n",
  483. ENV_FLAGS_VARACCESS_LOC);
  484. puts("\tFlag\tVariable Access Name\n");
  485. puts("\t----\t--------------------\n");
  486. env_flags_print_varaccess();
  487. puts("\n");
  488. /* Print the static flags that may exist */
  489. puts("Static flags:\n");
  490. printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
  491. "Variable Access");
  492. printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
  493. "---------------");
  494. env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags);
  495. puts("\n");
  496. /* walk through each variable and print the flags if non-default */
  497. puts("Active flags:\n");
  498. printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
  499. "Variable Access");
  500. printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
  501. "---------------");
  502. hwalk_r(&env_htab, print_active_flags);
  503. return 0;
  504. }
  505. #endif
  506. /*
  507. * Interactively edit an environment variable
  508. */
  509. #if defined(CONFIG_CMD_EDITENV)
  510. static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
  511. char * const argv[])
  512. {
  513. char buffer[CONFIG_SYS_CBSIZE];
  514. char *init_val;
  515. if (argc < 2)
  516. return CMD_RET_USAGE;
  517. /* Set read buffer to initial value or empty sting */
  518. init_val = getenv(argv[1]);
  519. if (init_val)
  520. sprintf(buffer, "%s", init_val);
  521. else
  522. buffer[0] = '\0';
  523. if (readline_into_buffer("edit: ", buffer, 0) < 0)
  524. return 1;
  525. return setenv(argv[1], buffer);
  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. char *addr, *cmd, *res;
  742. size_t size = 0;
  743. ssize_t len;
  744. env_t *envp;
  745. char sep = '\n';
  746. int chk = 0;
  747. int fmt = 0;
  748. cmd = *argv;
  749. while (--argc > 0 && **++argv == '-') {
  750. char *arg = *argv;
  751. while (*++arg) {
  752. switch (*arg) {
  753. case 'b': /* raw binary format */
  754. if (fmt++)
  755. goto sep_err;
  756. sep = '\0';
  757. break;
  758. case 'c': /* external checksum format */
  759. if (fmt++)
  760. goto sep_err;
  761. sep = '\0';
  762. chk = 1;
  763. break;
  764. case 's': /* size given */
  765. if (--argc <= 0)
  766. return cmd_usage(cmdtp);
  767. size = simple_strtoul(*++argv, NULL, 16);
  768. goto NXTARG;
  769. case 't': /* text format */
  770. if (fmt++)
  771. goto sep_err;
  772. sep = '\n';
  773. break;
  774. default:
  775. return CMD_RET_USAGE;
  776. }
  777. }
  778. NXTARG: ;
  779. }
  780. if (argc < 1)
  781. return CMD_RET_USAGE;
  782. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  783. if (size)
  784. memset(addr, '\0', size);
  785. argc--;
  786. argv++;
  787. if (sep) { /* export as text file */
  788. len = hexport_r(&env_htab, sep,
  789. H_MATCH_KEY | H_MATCH_IDENT,
  790. &addr, size, argc, argv);
  791. if (len < 0) {
  792. error("Cannot export environment: errno = %d\n", errno);
  793. return 1;
  794. }
  795. sprintf(buf, "%zX", (size_t)len);
  796. setenv("filesize", buf);
  797. return 0;
  798. }
  799. envp = (env_t *)addr;
  800. if (chk) /* export as checksum protected block */
  801. res = (char *)envp->data;
  802. else /* export as raw binary data */
  803. res = addr;
  804. len = hexport_r(&env_htab, '\0',
  805. H_MATCH_KEY | H_MATCH_IDENT,
  806. &res, ENV_SIZE, argc, argv);
  807. if (len < 0) {
  808. error("Cannot export environment: errno = %d\n", errno);
  809. return 1;
  810. }
  811. if (chk) {
  812. envp->crc = crc32(0, envp->data, ENV_SIZE);
  813. #ifdef CONFIG_ENV_ADDR_REDUND
  814. envp->flags = ACTIVE_FLAG;
  815. #endif
  816. }
  817. setenv_hex("filesize", len + offsetof(env_t, data));
  818. return 0;
  819. sep_err:
  820. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n", cmd);
  821. return 1;
  822. }
  823. #endif
  824. #ifdef CONFIG_CMD_IMPORTENV
  825. /*
  826. * env import [-d] [-t | -b | -c] addr [size]
  827. * -d: delete existing environment before importing;
  828. * otherwise overwrite / append to existion definitions
  829. * -t: assume text format; either "size" must be given or the
  830. * text data must be '\0' terminated
  831. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  832. * -c: assume checksum protected environment format
  833. * addr: memory address to read from
  834. * size: length of input data; if missing, proper '\0'
  835. * termination is mandatory
  836. */
  837. static int do_env_import(cmd_tbl_t *cmdtp, int flag,
  838. int argc, char * const argv[])
  839. {
  840. char *cmd, *addr;
  841. char sep = '\n';
  842. int chk = 0;
  843. int fmt = 0;
  844. int del = 0;
  845. size_t size;
  846. cmd = *argv;
  847. while (--argc > 0 && **++argv == '-') {
  848. char *arg = *argv;
  849. while (*++arg) {
  850. switch (*arg) {
  851. case 'b': /* raw binary format */
  852. if (fmt++)
  853. goto sep_err;
  854. sep = '\0';
  855. break;
  856. case 'c': /* external checksum format */
  857. if (fmt++)
  858. goto sep_err;
  859. sep = '\0';
  860. chk = 1;
  861. break;
  862. case 't': /* text format */
  863. if (fmt++)
  864. goto sep_err;
  865. sep = '\n';
  866. break;
  867. case 'd':
  868. del = 1;
  869. break;
  870. default:
  871. return CMD_RET_USAGE;
  872. }
  873. }
  874. }
  875. if (argc < 1)
  876. return CMD_RET_USAGE;
  877. if (!fmt)
  878. printf("## Warning: defaulting to text format\n");
  879. addr = (char *)simple_strtoul(argv[0], NULL, 16);
  880. if (argc == 2) {
  881. size = simple_strtoul(argv[1], NULL, 16);
  882. } else {
  883. char *s = addr;
  884. size = 0;
  885. while (size < MAX_ENV_SIZE) {
  886. if ((*s == sep) && (*(s+1) == '\0'))
  887. break;
  888. ++s;
  889. ++size;
  890. }
  891. if (size == MAX_ENV_SIZE) {
  892. printf("## Warning: Input data exceeds %d bytes"
  893. " - truncated\n", MAX_ENV_SIZE);
  894. }
  895. size += 2;
  896. printf("## Info: input data size = %zu = 0x%zX\n", size, size);
  897. }
  898. if (chk) {
  899. uint32_t crc;
  900. env_t *ep = (env_t *)addr;
  901. size -= offsetof(env_t, data);
  902. memcpy(&crc, &ep->crc, sizeof(crc));
  903. if (crc32(0, ep->data, size) != crc) {
  904. puts("## Error: bad CRC, import failed\n");
  905. return 1;
  906. }
  907. addr = (char *)ep->data;
  908. }
  909. if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
  910. 0, NULL) == 0) {
  911. error("Environment import failed: errno = %d\n", errno);
  912. return 1;
  913. }
  914. gd->flags |= GD_FLG_ENV_READY;
  915. return 0;
  916. sep_err:
  917. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  918. cmd);
  919. return 1;
  920. }
  921. #endif
  922. /*
  923. * New command line interface: "env" command with subcommands
  924. */
  925. static cmd_tbl_t cmd_env_sub[] = {
  926. #if defined(CONFIG_CMD_ASKENV)
  927. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  928. #endif
  929. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  930. U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
  931. #if defined(CONFIG_CMD_EDITENV)
  932. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  933. #endif
  934. #if defined(CONFIG_CMD_ENV_CALLBACK)
  935. U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
  936. #endif
  937. #if defined(CONFIG_CMD_ENV_FLAGS)
  938. U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
  939. #endif
  940. #if defined(CONFIG_CMD_EXPORTENV)
  941. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  942. #endif
  943. #if defined(CONFIG_CMD_GREPENV)
  944. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  945. #endif
  946. #if defined(CONFIG_CMD_IMPORTENV)
  947. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  948. #endif
  949. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  950. #if defined(CONFIG_CMD_RUN)
  951. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  952. #endif
  953. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  954. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  955. #endif
  956. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  957. };
  958. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  959. void env_reloc(void)
  960. {
  961. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  962. }
  963. #endif
  964. static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  965. {
  966. cmd_tbl_t *cp;
  967. if (argc < 2)
  968. return CMD_RET_USAGE;
  969. /* drop initial "env" arg */
  970. argc--;
  971. argv++;
  972. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  973. if (cp)
  974. return cp->cmd(cmdtp, flag, argc, argv);
  975. return CMD_RET_USAGE;
  976. }
  977. #ifdef CONFIG_SYS_LONGHELP
  978. static char env_help_text[] =
  979. #if defined(CONFIG_CMD_ASKENV)
  980. "ask name [message] [size] - ask for environment variable\nenv "
  981. #endif
  982. #if defined(CONFIG_CMD_ENV_CALLBACK)
  983. "callbacks - print callbacks and their associated variables\nenv "
  984. #endif
  985. "default [-f] -a - [forcibly] reset default environment\n"
  986. "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
  987. "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
  988. #if defined(CONFIG_CMD_EDITENV)
  989. "env edit name - edit environment variable\n"
  990. #endif
  991. #if defined(CONFIG_CMD_EXPORTENV)
  992. "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  993. #endif
  994. #if defined(CONFIG_CMD_ENV_FLAGS)
  995. "env flags - print variables that have non-default flags\n"
  996. #endif
  997. #if defined(CONFIG_CMD_GREPENV)
  998. #ifdef CONFIG_REGEX
  999. "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
  1000. #else
  1001. "env grep [-n | -v | -b] string [...] - search environment\n"
  1002. #endif
  1003. #endif
  1004. #if defined(CONFIG_CMD_IMPORTENV)
  1005. "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
  1006. #endif
  1007. "env print [-a | name ...] - print environment\n"
  1008. #if defined(CONFIG_CMD_RUN)
  1009. "env run var [...] - run commands in an environment variable\n"
  1010. #endif
  1011. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  1012. "env save - save environment\n"
  1013. #endif
  1014. "env set [-f] name [arg ...]\n";
  1015. #endif
  1016. U_BOOT_CMD(
  1017. env, CONFIG_SYS_MAXARGS, 1, do_env,
  1018. "environment handling commands", env_help_text
  1019. );
  1020. /*
  1021. * Old command line interface, kept for compatibility
  1022. */
  1023. #if defined(CONFIG_CMD_EDITENV)
  1024. U_BOOT_CMD_COMPLETE(
  1025. editenv, 2, 0, do_env_edit,
  1026. "edit environment variable",
  1027. "name\n"
  1028. " - edit environment variable 'name'",
  1029. var_complete
  1030. );
  1031. #endif
  1032. U_BOOT_CMD_COMPLETE(
  1033. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  1034. "print environment variables",
  1035. "[-a]\n - print [all] values of all environment variables\n"
  1036. "printenv name ...\n"
  1037. " - print value of environment variable 'name'",
  1038. var_complete
  1039. );
  1040. #ifdef CONFIG_CMD_GREPENV
  1041. U_BOOT_CMD_COMPLETE(
  1042. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  1043. "search environment variables",
  1044. #ifdef CONFIG_REGEX
  1045. "[-e] [-n | -v | -b] string ...\n"
  1046. #else
  1047. "[-n | -v | -b] string ...\n"
  1048. #endif
  1049. " - list environment name=value pairs matching 'string'\n"
  1050. #ifdef CONFIG_REGEX
  1051. " \"-e\": enable regular expressions;\n"
  1052. #endif
  1053. " \"-n\": search variable names; \"-v\": search values;\n"
  1054. " \"-b\": search both names and values (default)",
  1055. var_complete
  1056. );
  1057. #endif
  1058. U_BOOT_CMD_COMPLETE(
  1059. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  1060. "set environment variables",
  1061. "[-f] name value ...\n"
  1062. " - [forcibly] set environment variable 'name' to 'value ...'\n"
  1063. "setenv [-f] name\n"
  1064. " - [forcibly] delete environment variable 'name'",
  1065. var_complete
  1066. );
  1067. #if defined(CONFIG_CMD_ASKENV)
  1068. U_BOOT_CMD(
  1069. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  1070. "get environment variables from stdin",
  1071. "name [message] [size]\n"
  1072. " - get environment variable 'name' from stdin (max 'size' chars)"
  1073. );
  1074. #endif
  1075. #if defined(CONFIG_CMD_RUN)
  1076. U_BOOT_CMD_COMPLETE(
  1077. run, CONFIG_SYS_MAXARGS, 1, do_run,
  1078. "run commands in an environment variable",
  1079. "var [...]\n"
  1080. " - run the commands in the environment variable(s) 'var'",
  1081. var_complete
  1082. );
  1083. #endif
  1084. #endif /* CONFIG_SPL_BUILD */