nvedit.c 36 KB

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