nvedit.c 36 KB

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