cmd_nvedit.c 28 KB

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