cmd_nvedit.c 28 KB

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