nvedit.c 34 KB

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