cmd_nvedit.c 28 KB

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