nvedit.c 38 KB

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