cmd_nvedit.c 30 KB

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