nvedit.c 36 KB

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