nvedit.c 36 KB

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