nvedit.c 38 KB

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