nvedit.c 30 KB

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