nvedit.c 34 KB

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