nvedit.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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. size -= offsetof(env_t, data);
  1002. memcpy(&crc, &ep->crc, sizeof(crc));
  1003. if (crc32(0, ep->data, size) != crc) {
  1004. puts("## Error: bad CRC, import failed\n");
  1005. return 1;
  1006. }
  1007. ptr = (char *)ep->data;
  1008. }
  1009. if (!himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
  1010. crlf_is_lf, wl ? argc - 2 : 0, wl ? &argv[2] : NULL)) {
  1011. pr_err("## Error: Environment import failed: errno = %d\n",
  1012. errno);
  1013. return 1;
  1014. }
  1015. gd->flags |= GD_FLG_ENV_READY;
  1016. return 0;
  1017. sep_err:
  1018. printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
  1019. cmd);
  1020. return 1;
  1021. }
  1022. #endif
  1023. #if defined(CONFIG_CMD_NVEDIT_INFO)
  1024. /*
  1025. * print_env_info - print environment information
  1026. */
  1027. static int print_env_info(void)
  1028. {
  1029. const char *value;
  1030. /* print environment validity value */
  1031. switch (gd->env_valid) {
  1032. case ENV_INVALID:
  1033. value = "invalid";
  1034. break;
  1035. case ENV_VALID:
  1036. value = "valid";
  1037. break;
  1038. case ENV_REDUND:
  1039. value = "redundant";
  1040. break;
  1041. default:
  1042. value = "unknown";
  1043. break;
  1044. }
  1045. printf("env_valid = %s\n", value);
  1046. /* print environment ready flag */
  1047. value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
  1048. printf("env_ready = %s\n", value);
  1049. /* print environment using default flag */
  1050. value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
  1051. printf("env_use_default = %s\n", value);
  1052. return CMD_RET_SUCCESS;
  1053. }
  1054. #define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
  1055. #define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
  1056. /*
  1057. * env info - display environment information
  1058. * env info [-d] - evaluate whether default environment is used
  1059. * env info [-p] - evaluate whether environment can be persisted
  1060. * Add [-q] - quiet mode, use only for command result, for test by example:
  1061. * test env info -p -d -q
  1062. */
  1063. static int do_env_info(struct cmd_tbl *cmdtp, int flag,
  1064. int argc, char *const argv[])
  1065. {
  1066. int eval_flags = 0;
  1067. int eval_results = 0;
  1068. bool quiet = false;
  1069. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1070. enum env_location loc;
  1071. #endif
  1072. /* display environment information */
  1073. if (argc <= 1)
  1074. return print_env_info();
  1075. /* process options */
  1076. while (--argc > 0 && **++argv == '-') {
  1077. char *arg = *argv;
  1078. while (*++arg) {
  1079. switch (*arg) {
  1080. case 'd':
  1081. eval_flags |= ENV_INFO_IS_DEFAULT;
  1082. break;
  1083. case 'p':
  1084. eval_flags |= ENV_INFO_IS_PERSISTED;
  1085. break;
  1086. case 'q':
  1087. quiet = true;
  1088. break;
  1089. default:
  1090. return CMD_RET_USAGE;
  1091. }
  1092. }
  1093. }
  1094. /* evaluate whether default environment is used */
  1095. if (eval_flags & ENV_INFO_IS_DEFAULT) {
  1096. if (gd->flags & GD_FLG_ENV_DEFAULT) {
  1097. if (!quiet)
  1098. printf("Default environment is used\n");
  1099. eval_results |= ENV_INFO_IS_DEFAULT;
  1100. } else {
  1101. if (!quiet)
  1102. printf("Environment was loaded from persistent storage\n");
  1103. }
  1104. }
  1105. /* evaluate whether environment can be persisted */
  1106. if (eval_flags & ENV_INFO_IS_PERSISTED) {
  1107. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1108. loc = env_get_location(ENVOP_SAVE, gd->env_load_prio);
  1109. if (ENVL_NOWHERE != loc && ENVL_UNKNOWN != loc) {
  1110. if (!quiet)
  1111. printf("Environment can be persisted\n");
  1112. eval_results |= ENV_INFO_IS_PERSISTED;
  1113. } else {
  1114. if (!quiet)
  1115. printf("Environment cannot be persisted\n");
  1116. }
  1117. #else
  1118. if (!quiet)
  1119. printf("Environment cannot be persisted\n");
  1120. #endif
  1121. }
  1122. /* The result of evaluations is combined with AND */
  1123. if (eval_flags != eval_results)
  1124. return CMD_RET_FAILURE;
  1125. return CMD_RET_SUCCESS;
  1126. }
  1127. #endif
  1128. #if defined(CONFIG_CMD_ENV_EXISTS)
  1129. static int do_env_exists(struct cmd_tbl *cmdtp, int flag, int argc,
  1130. char *const argv[])
  1131. {
  1132. struct env_entry e, *ep;
  1133. if (argc < 2)
  1134. return CMD_RET_USAGE;
  1135. e.key = argv[1];
  1136. e.data = NULL;
  1137. hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
  1138. return (ep == NULL) ? 1 : 0;
  1139. }
  1140. #endif
  1141. /*
  1142. * New command line interface: "env" command with subcommands
  1143. */
  1144. static struct cmd_tbl cmd_env_sub[] = {
  1145. #if defined(CONFIG_CMD_ASKENV)
  1146. U_BOOT_CMD_MKENT(ask, CONFIG_SYS_MAXARGS, 1, do_env_ask, "", ""),
  1147. #endif
  1148. U_BOOT_CMD_MKENT(default, 1, 0, do_env_default, "", ""),
  1149. U_BOOT_CMD_MKENT(delete, CONFIG_SYS_MAXARGS, 0, do_env_delete, "", ""),
  1150. #if defined(CONFIG_CMD_EDITENV)
  1151. U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
  1152. #endif
  1153. #if defined(CONFIG_CMD_ENV_CALLBACK)
  1154. U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
  1155. #endif
  1156. #if defined(CONFIG_CMD_ENV_FLAGS)
  1157. U_BOOT_CMD_MKENT(flags, 1, 0, do_env_flags, "", ""),
  1158. #endif
  1159. #if defined(CONFIG_CMD_EXPORTENV)
  1160. U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
  1161. #endif
  1162. #if defined(CONFIG_CMD_GREPENV)
  1163. U_BOOT_CMD_MKENT(grep, CONFIG_SYS_MAXARGS, 1, do_env_grep, "", ""),
  1164. #endif
  1165. #if defined(CONFIG_CMD_IMPORTENV)
  1166. U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
  1167. #endif
  1168. #if defined(CONFIG_CMD_NVEDIT_INFO)
  1169. U_BOOT_CMD_MKENT(info, 3, 0, do_env_info, "", ""),
  1170. #endif
  1171. #if defined(CONFIG_CMD_NVEDIT_LOAD)
  1172. U_BOOT_CMD_MKENT(load, 1, 0, do_env_load, "", ""),
  1173. #endif
  1174. U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
  1175. #if defined(CONFIG_CMD_RUN)
  1176. U_BOOT_CMD_MKENT(run, CONFIG_SYS_MAXARGS, 1, do_run, "", ""),
  1177. #endif
  1178. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1179. U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
  1180. #if defined(CONFIG_CMD_ERASEENV)
  1181. U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
  1182. #endif
  1183. #endif
  1184. #if defined(CONFIG_CMD_NVEDIT_SELECT)
  1185. U_BOOT_CMD_MKENT(select, 2, 0, do_env_select, "", ""),
  1186. #endif
  1187. U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
  1188. #if defined(CONFIG_CMD_ENV_EXISTS)
  1189. U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
  1190. #endif
  1191. };
  1192. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  1193. void env_reloc(void)
  1194. {
  1195. fixup_cmdtable(cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  1196. }
  1197. #endif
  1198. static int do_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  1199. {
  1200. struct cmd_tbl *cp;
  1201. if (argc < 2)
  1202. return CMD_RET_USAGE;
  1203. /* drop initial "env" arg */
  1204. argc--;
  1205. argv++;
  1206. cp = find_cmd_tbl(argv[0], cmd_env_sub, ARRAY_SIZE(cmd_env_sub));
  1207. if (cp)
  1208. return cp->cmd(cmdtp, flag, argc, argv);
  1209. return CMD_RET_USAGE;
  1210. }
  1211. #ifdef CONFIG_SYS_LONGHELP
  1212. static char env_help_text[] =
  1213. #if defined(CONFIG_CMD_ASKENV)
  1214. "ask name [message] [size] - ask for environment variable\nenv "
  1215. #endif
  1216. #if defined(CONFIG_CMD_ENV_CALLBACK)
  1217. "callbacks - print callbacks and their associated variables\nenv "
  1218. #endif
  1219. "default [-f] -a - [forcibly] reset default environment\n"
  1220. "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
  1221. "env delete [-f] var [...] - [forcibly] delete variable(s)\n"
  1222. #if defined(CONFIG_CMD_EDITENV)
  1223. "env edit name - edit environment variable\n"
  1224. #endif
  1225. #if defined(CONFIG_CMD_ENV_EXISTS)
  1226. "env exists name - tests for existence of variable\n"
  1227. #endif
  1228. #if defined(CONFIG_CMD_EXPORTENV)
  1229. "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
  1230. #endif
  1231. #if defined(CONFIG_CMD_ENV_FLAGS)
  1232. "env flags - print variables that have non-default flags\n"
  1233. #endif
  1234. #if defined(CONFIG_CMD_GREPENV)
  1235. #ifdef CONFIG_REGEX
  1236. "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
  1237. #else
  1238. "env grep [-n | -v | -b] string [...] - search environment\n"
  1239. #endif
  1240. #endif
  1241. #if defined(CONFIG_CMD_IMPORTENV)
  1242. "env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
  1243. #endif
  1244. #if defined(CONFIG_CMD_NVEDIT_INFO)
  1245. "env info - display environment information\n"
  1246. "env info [-d] [-p] [-q] - evaluate environment information\n"
  1247. " \"-d\": default environment is used\n"
  1248. " \"-p\": environment can be persisted\n"
  1249. " \"-q\": quiet output\n"
  1250. #endif
  1251. "env print [-a | name ...] - print environment\n"
  1252. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1253. "env print -e [-guid guid] [-n] [name ...] - print UEFI environment\n"
  1254. #endif
  1255. #if defined(CONFIG_CMD_RUN)
  1256. "env run var [...] - run commands in an environment variable\n"
  1257. #endif
  1258. #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
  1259. "env save - save environment\n"
  1260. #if defined(CONFIG_CMD_ERASEENV)
  1261. "env erase - erase environment\n"
  1262. #endif
  1263. #endif
  1264. #if defined(CONFIG_CMD_NVEDIT_LOAD)
  1265. "env load - load environment\n"
  1266. #endif
  1267. #if defined(CONFIG_CMD_NVEDIT_SELECT)
  1268. "env select [target] - select environment target\n"
  1269. #endif
  1270. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1271. "env set -e [-nv][-bs][-rt][-at][-a][-i addr,size][-v] name [arg ...]\n"
  1272. " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
  1273. #endif
  1274. "env set [-f] name [arg ...]\n";
  1275. #endif
  1276. U_BOOT_CMD(
  1277. env, CONFIG_SYS_MAXARGS, 1, do_env,
  1278. "environment handling commands", env_help_text
  1279. );
  1280. /*
  1281. * Old command line interface, kept for compatibility
  1282. */
  1283. #if defined(CONFIG_CMD_EDITENV)
  1284. U_BOOT_CMD_COMPLETE(
  1285. editenv, 2, 0, do_env_edit,
  1286. "edit environment variable",
  1287. "name\n"
  1288. " - edit environment variable 'name'",
  1289. var_complete
  1290. );
  1291. #endif
  1292. U_BOOT_CMD_COMPLETE(
  1293. printenv, CONFIG_SYS_MAXARGS, 1, do_env_print,
  1294. "print environment variables",
  1295. "[-a]\n - print [all] values of all environment variables\n"
  1296. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1297. "printenv -e [-guid guid][-n] [name ...]\n"
  1298. " - print UEFI variable 'name' or all the variables\n"
  1299. " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
  1300. " \"-n\": suppress dumping variable's value\n"
  1301. #endif
  1302. "printenv name ...\n"
  1303. " - print value of environment variable 'name'",
  1304. var_complete
  1305. );
  1306. #ifdef CONFIG_CMD_GREPENV
  1307. U_BOOT_CMD_COMPLETE(
  1308. grepenv, CONFIG_SYS_MAXARGS, 0, do_env_grep,
  1309. "search environment variables",
  1310. #ifdef CONFIG_REGEX
  1311. "[-e] [-n | -v | -b] string ...\n"
  1312. #else
  1313. "[-n | -v | -b] string ...\n"
  1314. #endif
  1315. " - list environment name=value pairs matching 'string'\n"
  1316. #ifdef CONFIG_REGEX
  1317. " \"-e\": enable regular expressions;\n"
  1318. #endif
  1319. " \"-n\": search variable names; \"-v\": search values;\n"
  1320. " \"-b\": search both names and values (default)",
  1321. var_complete
  1322. );
  1323. #endif
  1324. U_BOOT_CMD_COMPLETE(
  1325. setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
  1326. "set environment variables",
  1327. #if defined(CONFIG_CMD_NVEDIT_EFI)
  1328. "-e [-guid guid][-nv][-bs][-rt][-at][-a][-v]\n"
  1329. " [-i addr,size name], or [name [value ...]]\n"
  1330. " - set UEFI variable 'name' to 'value' ...'\n"
  1331. " \"-guid\": GUID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"
  1332. " \"-nv\": set non-volatile attribute\n"
  1333. " \"-bs\": set boot-service attribute\n"
  1334. " \"-rt\": set runtime attribute\n"
  1335. " \"-at\": set time-based authentication attribute\n"
  1336. " \"-a\": append-write\n"
  1337. " \"-i addr,size\": use <addr,size> as variable's value\n"
  1338. " \"-v\": verbose message\n"
  1339. " - delete UEFI variable 'name' if 'value' not specified\n"
  1340. #endif
  1341. "setenv [-f] name value ...\n"
  1342. " - [forcibly] set environment variable 'name' to 'value ...'\n"
  1343. "setenv [-f] name\n"
  1344. " - [forcibly] delete environment variable 'name'",
  1345. var_complete
  1346. );
  1347. #if defined(CONFIG_CMD_ASKENV)
  1348. U_BOOT_CMD(
  1349. askenv, CONFIG_SYS_MAXARGS, 1, do_env_ask,
  1350. "get environment variables from stdin",
  1351. "name [message] [size]\n"
  1352. " - get environment variable 'name' from stdin (max 'size' chars)"
  1353. );
  1354. #endif
  1355. #if defined(CONFIG_CMD_RUN)
  1356. U_BOOT_CMD_COMPLETE(
  1357. run, CONFIG_SYS_MAXARGS, 1, do_run,
  1358. "run commands in an environment variable",
  1359. "var [...]\n"
  1360. " - run the commands in the environment variable(s) 'var'",
  1361. var_complete
  1362. );
  1363. #endif
  1364. #endif /* CONFIG_SPL_BUILD */