cmd_nvedit.c 29 KB

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