cmd_nvedit.c 28 KB

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