cmd_nvedit.c 29 KB

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