nvedit.c 35 KB

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