nvedit.c 30 KB

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