nvedit.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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. #if defined(CONFIG_CMD_NVEDIT_LOAD)
  671. static int do_env_load(struct cmd_tbl *cmdtp, int flag, int argc,
  672. char *const argv[])
  673. {
  674. return env_reload() ? 1 : 0;
  675. }
  676. #endif
  677. #if defined(CONFIG_CMD_NVEDIT_SELECT)
  678. static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc,
  679. char *const argv[])
  680. {
  681. return env_select(argv[1]) ? 1 : 0;
  682. }
  683. #endif
  684. #endif /* CONFIG_SPL_BUILD */
  685. int env_match(uchar *s1, int i2)
  686. {
  687. if (s1 == NULL)
  688. return -1;
  689. while (*s1 == env_get_char(i2++))
  690. if (*s1++ == '=')
  691. return i2;
  692. if (*s1 == '\0' && env_get_char(i2-1) == '=')
  693. return i2;
  694. return -1;
  695. }
  696. #ifndef CONFIG_SPL_BUILD
  697. static int do_env_default(struct cmd_tbl *cmdtp, int flag,
  698. int argc, char *const argv[])
  699. {
  700. int all = 0, env_flag = H_INTERACTIVE;
  701. debug("Initial value for argc=%d\n", argc);
  702. while (--argc > 0 && **++argv == '-') {
  703. char *arg = *argv;
  704. while (*++arg) {
  705. switch (*arg) {
  706. case 'a': /* default all */
  707. all = 1;
  708. break;
  709. case 'f': /* force */
  710. env_flag |= H_FORCE;
  711. break;
  712. default:
  713. return cmd_usage(cmdtp);
  714. }
  715. }
  716. }
  717. debug("Final value for argc=%d\n", argc);
  718. if (all && (argc == 0)) {
  719. /* Reset the whole environment */
  720. env_set_default("## Resetting to default environment\n",
  721. env_flag);
  722. return 0;
  723. }
  724. if (!all && (argc > 0)) {
  725. /* Reset individual variables */
  726. env_set_default_vars(argc, argv, env_flag);
  727. return 0;
  728. }
  729. return cmd_usage(cmdtp);
  730. }
  731. static int do_env_delete(struct cmd_tbl *cmdtp, int flag,
  732. int argc, char *const argv[])
  733. {
  734. int env_flag = H_INTERACTIVE;
  735. int ret = 0;
  736. debug("Initial value for argc=%d\n", argc);
  737. while (argc > 1 && **(argv + 1) == '-') {
  738. char *arg = *++argv;
  739. --argc;
  740. while (*++arg) {
  741. switch (*arg) {
  742. case 'f': /* force */
  743. env_flag |= H_FORCE;
  744. break;
  745. default:
  746. return CMD_RET_USAGE;
  747. }
  748. }
  749. }
  750. debug("Final value for argc=%d\n", argc);
  751. env_id++;
  752. while (--argc > 0) {
  753. char *name = *++argv;
  754. if (!hdelete_r(name, &env_htab, env_flag))
  755. ret = 1;
  756. }
  757. return ret;
  758. }
  759. #ifdef CONFIG_CMD_EXPORTENV
  760. /*
  761. * env export [-t | -b | -c] [-s size] addr [var ...]
  762. * -t: export as text format; if size is given, data will be
  763. * padded with '\0' bytes; if not, one terminating '\0'
  764. * will be added (which is included in the "filesize"
  765. * setting so you can for exmple copy this to flash and
  766. * keep the termination).
  767. * -b: export as binary format (name=value pairs separated by
  768. * '\0', list end marked by double "\0\0")
  769. * -c: export as checksum protected environment format as
  770. * used for example by "saveenv" command
  771. * -s size:
  772. * size of output buffer
  773. * addr: memory address where environment gets stored
  774. * var... List of variable names that get included into the
  775. * export. Without arguments, the whole environment gets
  776. * exported.
  777. *
  778. * With "-c" and size is NOT given, then the export command will
  779. * format the data as currently used for the persistent storage,
  780. * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
  781. * prepend a valid CRC32 checksum and, in case of redundant
  782. * environment, a "current" redundancy flag. If size is given, this
  783. * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
  784. * checksum and redundancy flag will be inserted.
  785. *
  786. * With "-b" and "-t", always only the real data (including a
  787. * terminating '\0' byte) will be written; here the optional size
  788. * argument will be used to make sure not to overflow the user
  789. * provided buffer; the command will abort if the size is not
  790. * sufficient. Any remaining space will be '\0' padded.
  791. *
  792. * On successful return, the variable "filesize" will be set.
  793. * Note that filesize includes the trailing/terminating '\0' byte(s).
  794. *
  795. * Usage scenario: create a text snapshot/backup of the current settings:
  796. *
  797. * => env export -t 100000
  798. * => era ${backup_addr} +${filesize}
  799. * => cp.b 100000 ${backup_addr} ${filesize}
  800. *
  801. * Re-import this snapshot, deleting all other settings:
  802. *
  803. * => env import -d -t ${backup_addr}
  804. */
  805. static int do_env_export(struct cmd_tbl *cmdtp, int flag,
  806. int argc, char *const argv[])
  807. {
  808. char buf[32];
  809. ulong addr;
  810. char *ptr, *cmd, *res;
  811. size_t size = 0;
  812. ssize_t len;
  813. env_t *envp;
  814. char sep = '\n';
  815. int chk = 0;
  816. int fmt = 0;
  817. cmd = *argv;
  818. while (--argc > 0 && **++argv == '-') {
  819. char *arg = *argv;
  820. while (*++arg) {
  821. switch (*arg) {
  822. case 'b': /* raw binary format */
  823. if (fmt++)
  824. goto sep_err;
  825. sep = '\0';
  826. break;
  827. case 'c': /* external checksum format */
  828. if (fmt++)
  829. goto sep_err;
  830. sep = '\0';
  831. chk = 1;
  832. break;
  833. case 's': /* size given */
  834. if (--argc <= 0)
  835. return cmd_usage(cmdtp);
  836. size = simple_strtoul(*++argv, NULL, 16);
  837. goto NXTARG;
  838. case 't': /* text format */
  839. if (fmt++)
  840. goto sep_err;
  841. sep = '\n';
  842. break;
  843. default:
  844. return CMD_RET_USAGE;
  845. }
  846. }
  847. NXTARG: ;
  848. }
  849. if (argc < 1)
  850. return CMD_RET_USAGE;
  851. addr = simple_strtoul(argv[0], NULL, 16);
  852. ptr = map_sysmem(addr, size);
  853. if (size)
  854. memset(ptr, '\0', size);
  855. argc--;
  856. argv++;
  857. if (sep) { /* export as text file */
  858. len = hexport_r(&env_htab, sep,
  859. H_MATCH_KEY | H_MATCH_IDENT,
  860. &ptr, size, argc, argv);
  861. if (len < 0) {
  862. pr_err("## Error: Cannot export environment: errno = %d\n",
  863. errno);
  864. return 1;
  865. }
  866. sprintf(buf, "%zX", (size_t)len);
  867. env_set("filesize", buf);
  868. return 0;
  869. }
  870. envp = (env_t *)ptr;
  871. if (chk) /* export as checksum protected block */
  872. res = (char *)envp->data;
  873. else /* export as raw binary data */
  874. res = ptr;
  875. len = hexport_r(&env_htab, '\0',
  876. H_MATCH_KEY | H_MATCH_IDENT,
  877. &res, ENV_SIZE, argc, argv);
  878. if (len < 0) {
  879. pr_err("## Error: Cannot export environment: errno = %d\n",
  880. errno);
  881. return 1;
  882. }
  883. if (chk) {
  884. envp->crc = crc32(0, envp->data,
  885. size ? size - offsetof(env_t, data) : ENV_SIZE);
  886. #ifdef CONFIG_ENV_ADDR_REDUND
  887. envp->flags = ENV_REDUND_ACTIVE;
  888. #endif
  889. }
  890. env_set_hex("filesize", len + offsetof(env_t, data));
  891. return 0;
  892. sep_err:
  893. printf("## Error: %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  894. cmd);
  895. return 1;
  896. }
  897. #endif
  898. #ifdef CONFIG_CMD_IMPORTENV
  899. /*
  900. * env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
  901. * -d: delete existing environment before importing if no var is
  902. * passed; if vars are passed, if one var is in the current
  903. * environment but not in the environment at addr, delete var from
  904. * current environment;
  905. * otherwise overwrite / append to existing definitions
  906. * -t: assume text format; either "size" must be given or the
  907. * text data must be '\0' terminated
  908. * -r: handle CRLF like LF, that means exported variables with
  909. * a content which ends with \r won't get imported. Used
  910. * to import text files created with editors which are using CRLF
  911. * for line endings. Only effective in addition to -t.
  912. * -b: assume binary format ('\0' separated, "\0\0" terminated)
  913. * -c: assume checksum protected environment format
  914. * addr: memory address to read from
  915. * size: length of input data; if missing, proper '\0'
  916. * termination is mandatory
  917. * if var is set and size should be missing (i.e. '\0'
  918. * termination), set size to '-'
  919. * var... List of the names of the only variables that get imported from
  920. * the environment at address 'addr'. Without arguments, the whole
  921. * environment gets imported.
  922. */
  923. static int do_env_import(struct cmd_tbl *cmdtp, int flag,
  924. int argc, char *const argv[])
  925. {
  926. ulong addr;
  927. char *cmd, *ptr;
  928. char sep = '\n';
  929. int chk = 0;
  930. int fmt = 0;
  931. int del = 0;
  932. int crlf_is_lf = 0;
  933. int wl = 0;
  934. size_t size;
  935. cmd = *argv;
  936. while (--argc > 0 && **++argv == '-') {
  937. char *arg = *argv;
  938. while (*++arg) {
  939. switch (*arg) {
  940. case 'b': /* raw binary format */
  941. if (fmt++)
  942. goto sep_err;
  943. sep = '\0';
  944. break;
  945. case 'c': /* external checksum format */
  946. if (fmt++)
  947. goto sep_err;
  948. sep = '\0';
  949. chk = 1;
  950. break;
  951. case 't': /* text format */
  952. if (fmt++)
  953. goto sep_err;
  954. sep = '\n';
  955. break;
  956. case 'r': /* handle CRLF like LF */
  957. crlf_is_lf = 1;
  958. break;
  959. case 'd':
  960. del = 1;
  961. break;
  962. default:
  963. return CMD_RET_USAGE;
  964. }
  965. }
  966. }
  967. if (argc < 1)
  968. return CMD_RET_USAGE;
  969. if (!fmt)
  970. printf("## Warning: defaulting to text format\n");
  971. if (sep != '\n' && crlf_is_lf )
  972. crlf_is_lf = 0;
  973. addr = simple_strtoul(argv[0], NULL, 16);
  974. ptr = map_sysmem(addr, 0);
  975. if (argc >= 2 && strcmp(argv[1], "-")) {
  976. size = simple_strtoul(argv[1], NULL, 16);
  977. } else if (chk) {
  978. puts("## Error: external checksum format must pass size\n");
  979. return CMD_RET_FAILURE;
  980. } else {
  981. char *s = ptr;
  982. size = 0;
  983. while (size < MAX_ENV_SIZE) {
  984. if ((*s == sep) && (*(s+1) == '\0'))
  985. break;
  986. ++s;
  987. ++size;
  988. }
  989. if (size == MAX_ENV_SIZE) {
  990. printf("## Warning: Input data exceeds %d bytes"
  991. " - truncated\n", MAX_ENV_SIZE);
  992. }
  993. size += 2;
  994. printf("## Info: input data size = %zu = 0x%zX\n", size, size);
  995. }
  996. if (argc > 2)
  997. wl = 1;
  998. if (chk) {
  999. uint32_t crc;
  1000. env_t *ep = (env_t *)ptr;
  1001. if (size <= offsetof(env_t, data)) {
  1002. printf("## Error: Invalid size 0x%zX\n", size);
  1003. return 1;
  1004. }
  1005. size -= offsetof(env_t, data);
  1006. memcpy(&crc, &ep->crc, sizeof(crc));
  1007. if (crc32(0, ep->data, size) != crc) {
  1008. puts("## Error: bad CRC, import failed\n");
  1009. return 1;
  1010. }
  1011. ptr = (char *)ep->data;
  1012. }
  1013. if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
  1014. crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
  1015. pr_err("## Error: Environment import failed: errno = %d\n",
  1016. errno);
  1017. return 1;
  1018. }
  1019. gd->flags |= GD_FLG_ENV_READY;
  1020. return 0;
  1021. sep_err:
  1022. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  1023. cmd);
  1024. return 1;
  1025. }
  1026. #endif
  1027. #if defined(CONFIG_CMD_NVEDIT_INFO)
  1028. /*
  1029. * print_env_info - print environment information
  1030. */
  1031. static int print_env_info(void)
  1032. {
  1033. const char *value;
  1034. /* print environment validity value */
  1035. switch (gd->env_valid) {
  1036. case ENV_INVALID:
  1037. value = "invalid";
  1038. break;
  1039. case ENV_VALID:
  1040. value = "valid";
  1041. break;
  1042. case ENV_REDUND:
  1043. value = "redundant";
  1044. break;
  1045. default:
  1046. value = "unknown";
  1047. break;
  1048. }
  1049. printf("env_valid = %s\n", value);
  1050. /* print environment ready flag */
  1051. value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
  1052. printf("env_ready = %s\n", value);
  1053. /* print environment using default flag */
  1054. value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
  1055. printf("env_use_default = %s\n", value);
  1056. return CMD_RET_SUCCESS;
  1057. }
  1058. #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
  1059. #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
  1060. /*
  1061. * env info - display environment information
  1062. * env info [-d] - evaluate whether default environment is used
  1063. * env info [-p] - evaluate whether environment can be persisted
  1064. * Add [-q] - quiet mode, use only for command result, for test by example:
  1065. * test env info -p -d -q
  1066. */
  1067. static int do_env_info(struct cmd_tbl *cmdtp, int flag,
  1068. int argc, char *const argv[])
  1069. {
  1070. int eval_flags = 0;
  1071. int eval_results = 0;
  1072. bool quiet = false;
  1073. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1074. enum env_location loc;
  1075. #endif
  1076. /* display environment information */
  1077. if (argc <= 1)
  1078. return print_env_info();
  1079. /* process options */
  1080. while (--argc > 0 && **++argv == '-') {
  1081. char *arg = *argv;
  1082. while (*++arg) {
  1083. switch (*arg) {
  1084. case 'd':
  1085. eval_flags |= ENV_INFO_IS_DEFAULT;
  1086. break;
  1087. case 'p':
  1088. eval_flags |= ENV_INFO_IS_PERSISTED;
  1089. break;
  1090. case 'q':
  1091. quiet = true;
  1092. break;
  1093. default:
  1094. return CMD_RET_USAGE;
  1095. }
  1096. }
  1097. }
  1098. /* evaluate whether default environment is used */
  1099. if (eval_flags & ENV_INFO_IS_DEFAULT) {
  1100. if (gd->flags & GD_FLG_ENV_DEFAULT) {
  1101. if (!quiet)
  1102. printf("Default environment is used\n");
  1103. eval_results |= ENV_INFO_IS_DEFAULT;
  1104. } else {
  1105. if (!quiet)
  1106. printf("Environment was loaded from persistent storage\n");
  1107. }
  1108. }
  1109. /* evaluate whether environment can be persisted */
  1110. if (eval_flags & ENV_INFO_IS_PERSISTED) {
  1111. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1112. loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
  1113. if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
  1114. if (!quiet)
  1115. printf("Environment can be persisted\n");
  1116. eval_results |= ENV_INFO_IS_PERSISTED;
  1117. } else {
  1118. if (!quiet)
  1119. printf("Environment cannot be persisted\n");
  1120. }
  1121. #else
  1122. if (!quiet)
  1123. printf("Environment cannot be persisted\n");
  1124. #endif
  1125. }
  1126. /* The result of evaluations is combined with AND */
  1127. if (eval_flags != eval_results)
  1128. return CMD_RET_FAILURE;
  1129. return CMD_RET_SUCCESS;
  1130. }
  1131. #endif
  1132. #if defined(CONFIG_CMD_ENV_EXISTS)
  1133. static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
  1134. char *const argv[])
  1135. {
  1136. struct env_entry e, *ep;
  1137. if (argc < 2)
  1138. return CMD_RET_USAGE;
  1139. e.key = argv[1];
  1140. e.data = NULL;
  1141. hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
  1142. return (ep == NULL) ? 1 : 0;
  1143. }
  1144. #endif
  1145. /*
  1146. * New command line interface: "env" command with subcommands
  1147. */
  1148. static struct cmd_tbl cmd_env_sub[] = {
  1149. #if defined(CONFIG_CMD_ASKENV)
  1150. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  1151. #endif
  1152. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  1153. U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
  1154. #if defined(CONFIG_CMD_EDITENV)
  1155. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  1156. #endif
  1157. #if defined(CONFIG_CMD_ENV_CALLBACK)
  1158. U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
  1159. #endif
  1160. #if defined(CONFIG_CMD_ENV_FLAGS)
  1161. U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
  1162. #endif
  1163. #if defined(CONFIG_CMD_EXPORTENV)
  1164. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  1165. #endif
  1166. #if defined(CONFIG_CMD_GREPENV)
  1167. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  1168. #endif
  1169. #if defined(CONFIG_CMD_IMPORTENV)
  1170. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  1171. #endif
  1172. #if defined(CONFIG_CMD_NVEDIT_INFO)
  1173. U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
  1174. #endif
  1175. #if defined(CONFIG_CMD_NVEDIT_LOAD)
  1176. U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
  1177. #endif
  1178. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  1179. #if defined(CONFIG_CMD_RUN)
  1180. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  1181. #endif
  1182. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1183. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  1184. #if defined(CONFIG_CMD_ERASEENV)
  1185. U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
  1186. #endif
  1187. #endif
  1188. #if defined(CONFIG_CMD_NVEDIT_SELECT)
  1189. U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
  1190. #endif
  1191. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  1192. #if defined(CONFIG_CMD_ENV_EXISTS)
  1193. U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
  1194. #endif
  1195. };
  1196. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  1197. void env_reloc(void)
  1198. {
  1199. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  1200. }
  1201. #endif
  1202. static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  1203. {
  1204. struct cmd_tbl *cp;
  1205. if (argc < 2)
  1206. return CMD_RET_USAGE;
  1207. /* drop initial "env" arg */
  1208. argc--;
  1209. argv++;
  1210. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  1211. if (cp)
  1212. return cp->cmd(cmdtp, flag, argc, argv);
  1213. return CMD_RET_USAGE;
  1214. }
  1215. #ifdef CONFIG_SYS_LONGHELP
  1216. static char env_help_text[] =
  1217. #if defined(CONFIG_CMD_ASKENV)
  1218. "ask name [message] [size] - ask for environment variable\nenv "
  1219. #endif
  1220. #if defined(CONFIG_CMD_ENV_CALLBACK)
  1221. "callbacks - print callbacks and their associated variables\nenv "
  1222. #endif
  1223. "default [-f] -a - [forcibly] reset default environment\n"
  1224. "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
  1225. "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
  1226. #if defined(CONFIG_CMD_EDITENV)
  1227. "env edit name - edit environment variable\n"
  1228. #endif
  1229. #if defined(CONFIG_CMD_ENV_EXISTS)
  1230. "env exists name - tests for existence of variable\n"
  1231. #endif
  1232. #if defined(CONFIG_CMD_EXPORTENV)
  1233. "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  1234. #endif
  1235. #if defined(CONFIG_CMD_ENV_FLAGS)
  1236. "env flags - print variables that have non-default flags\n"
  1237. #endif
  1238. #if defined(CONFIG_CMD_GREPENV)
  1239. #ifdef CONFIG_REGEX
  1240. "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
  1241. #else
  1242. "env grep [-n | -v | -b] string [...] - search environment\n"
  1243. #endif
  1244. #endif
  1245. #if defined(CONFIG_CMD_IMPORTENV)
  1246. "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
  1247. #endif
  1248. #if defined(CONFIG_CMD_NVEDIT_INFO)
  1249. "env info - display environment information\n"
  1250. "env info [-d] [-p] [-q] - evaluate environment information\n"
  1251. " \"-d\": default environment is used\n"
  1252. " \"-p\": environment can be persisted\n"
  1253. " \"-q\": quiet output\n"
  1254. #endif
  1255. "env print [-a | name ...] - print environment\n"
  1256. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1257. "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
  1258. #endif
  1259. #if defined(CONFIG_CMD_RUN)
  1260. "env run var [...] - run commands in an environment variable\n"
  1261. #endif
  1262. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1263. "env save - save environment\n"
  1264. #if defined(CONFIG_CMD_ERASEENV)
  1265. "env erase - erase environment\n"
  1266. #endif
  1267. #endif
  1268. #if defined(CONFIG_CMD_NVEDIT_LOAD)
  1269. "env load - load environment\n"
  1270. #endif
  1271. #if defined(CONFIG_CMD_NVEDIT_SELECT)
  1272. "env select [target] - select environment target\n"
  1273. #endif
  1274. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1275. "env set -e [-nv][-bs][-rt][-at][-a][-i addr:size][-v] name [arg ...]\n"
  1276. " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
  1277. #endif
  1278. "env set [-f] name [arg ...]\n";
  1279. #endif
  1280. U_BOOT_CMD(
  1281. env, CONFIG_SYS_MAXARGS, 1, do_env,
  1282. "environment handling commands", env_help_text
  1283. );
  1284. /*
  1285. * Old command line interface, kept for compatibility
  1286. */
  1287. #if defined(CONFIG_CMD_EDITENV)
  1288. U_BOOT_CMD_COMPLETE(
  1289. editenv, 2, 0, do_env_edit,
  1290. "edit environment variable",
  1291. "name\n"
  1292. " - edit environment variable 'name'",
  1293. var_complete
  1294. );
  1295. #endif
  1296. U_BOOT_CMD_COMPLETE(
  1297. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  1298. "print environment variables",
  1299. "[-a]\n - print [all] values of all environment variables\n"
  1300. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1301. "printenv -e [-guid guid][-n] [name ...]\n"
  1302. " - print UEFI variable 'name' or all the variables\n"
  1303. " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
  1304. " \"-n\": suppress dumping variable's value\n"
  1305. #endif
  1306. "printenv name ...\n"
  1307. " - print value of environment variable 'name'",
  1308. var_complete
  1309. );
  1310. #ifdef CONFIG_CMD_GREPENV
  1311. U_BOOT_CMD_COMPLETE(
  1312. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  1313. "search environment variables",
  1314. #ifdef CONFIG_REGEX
  1315. "[-e] [-n | -v | -b] string ...\n"
  1316. #else
  1317. "[-n | -v | -b] string ...\n"
  1318. #endif
  1319. " - list environment name=value pairs matching 'string'\n"
  1320. #ifdef CONFIG_REGEX
  1321. " \"-e\": enable regular expressions;\n"
  1322. #endif
  1323. " \"-n\": search variable names; \"-v\": search values;\n"
  1324. " \"-b\": search both names and values (default)",
  1325. var_complete
  1326. );
  1327. #endif
  1328. U_BOOT_CMD_COMPLETE(
  1329. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  1330. "set environment variables",
  1331. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1332. "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
  1333. " [-i addr:size name], or [name [value ...]]\n"
  1334. " - set UEFI variable 'name' to 'value' ...'\n"
  1335. " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
  1336. " \"-nv\": set non-volatile attribute\n"
  1337. " \"-bs\": set boot-service attribute\n"
  1338. " \"-rt\": set runtime attribute\n"
  1339. " \"-at\": set time-based authentication attribute\n"
  1340. " \"-a\": append-write\n"
  1341. " \"-i addr,size\": use <addr,size> as variable's value\n"
  1342. " \"-v\": verbose message\n"
  1343. " - delete UEFI variable 'name' if 'value' not specified\n"
  1344. #endif
  1345. "setenv [-f] name value ...\n"
  1346. " - [forcibly] set environment variable 'name' to 'value ...'\n"
  1347. "setenv [-f] name\n"
  1348. " - [forcibly] delete environment variable 'name'",
  1349. var_complete
  1350. );
  1351. #if defined(CONFIG_CMD_ASKENV)
  1352. U_BOOT_CMD(
  1353. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  1354. "get environment variables from stdin",
  1355. "name [message] [size]\n"
  1356. " - get environment variable 'name' from stdin (max 'size' chars)"
  1357. );
  1358. #endif
  1359. #if defined(CONFIG_CMD_RUN)
  1360. U_BOOT_CMD_COMPLETE(
  1361. run, CONFIG_SYS_MAXARGS, 1, do_run,
  1362. "run commands in an environment variable",
  1363. "var [...]\n"
  1364. " - run the commands in the environment variable(s) 'var'",
  1365. var_complete
  1366. );
  1367. #endif
  1368. #endif /* CONFIG_SPL_BUILD */