nvedit.c 37 KB

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