cmd_nvedit.c 30 KB

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